JAXB Tip: one line of code to marshall and unmarshall xml.
4 Comments Published May 26th, 2011 in Java Tags: Java, JAXB, quick tip, shortening code, tip, XML.I wrote in a previous article how easy it is to translate Java object to/from XML, without adding exotic library dependencies to your project; recently I’ve going through the code of one of my colleague and I discovered that the 3 line necessary to marshal and un-marshal the XML can be shortened to a single line using the JAXB utility class:
JAXBContext context = JAXBContext.newInstance(ObjectToConvert.class); Unmarshaller u = context.createUnmarshaller(); return (ObjectToConvert) u.unmarshal(xmlInputStream); // becomes return (ClassToConvert)JAXB.unmarshal(xmlInputStream, ObjectToConvert.class); // and JAXBContext context = JAXBContext.newInstance(objectInstanceToConvert.getClass()); Marshaller m = jc.createMarshaller(); m.marshal(objectInstanceToConvert, xmlOutputStream); // becomes JAXB.marshall(objectInstanceToConvert, xmlOutputStream)
Nice: one line of code to convert Java objects from/to XML. I didn’t notice this utility method in the JAXB library at first, so I was implementing those two methods in my code, more or less the same way. It’s always good to remove code.
4 Responses to “JAXB Tip: one line of code to marshall and unmarshall xml.”
- 1 Pingback on May 29th, 2011 at 08:05
Leave a Reply
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « Feb | Jun » | |||||
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 | |||||
Follow me
Archives
Categories
- Android (3)
- Apple (29)
- Books (7)
- Eclipse (14)
- Errors (5)
- Firefox (7)
- Git (3)
- Hardware (18)
- Horror Code (8)
- Internet (21)
- Java (104)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (26)
- Linux (52)
- Opinions (26)
- OSX (11)
- OWNER API (2)
- Python (1)
- Software (33)
- Speeches and Conferences (8)
- Unix (5)
- Web (23)
- Windows (19)
Tag Cloud
Android apple architecture Bash configuration CSS Development Düsseldorf Eclipse Git Google Hardware hdr How-To howto Java JAXB job Karmic Linux lion MacBook music Open Source Opinion OSX os x patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu unix video Web Workaround XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links


















Hi Luigi,
One thing to be aware of is that while using the JAXB class requires less lines of code, it does not provide the best performance. Normally the metadata processing is done at context creation time, then since the JAXBContext is thread safe it can be reused. When you use the JAXB class, the metadata initialization hit is taken for each marshal and unmarshal opertation.
-Blaise
In addition, in at least some implementations of JAXB, each call to create a JAXBContext object will result in the creation of an anonymous class that does not get garbage collected. So over time, you’ll run out of memory (PermGen, presumably, though I haven’t tried to reproduce the problem directly in order to confirm that). If you’re just running the occasional unmarshal operation, you won’t care about either of these things, but if you’re doing lots of unmarshal operations, then reusing a single JAXBContext will make a big difference in both respects.
References:
http://whileonefork.blogspot.com/2010/09/leaking-of-jaxb.html
http://stackoverflow.com/questions/3584821/jaxbcontext-newinstance-memory-leak
Awesome comment Tim, thanks for sharing.