JAXB - Unmarshal XML String into Java Object
1 min
When trying to unmarshal XML to a Java object using JAXB you might want to pass the XML as a String
. However, the unmarshal()
method of the Unmarshaller
interface does not support passing an XML String.
Following code sample illustrates how to solve this.
Wrap the XML String in a StringReader
object and pass this to the unmarshal()
method as shown below.
Note that an even shorter one-liner notation is also possible.
public static Car unmarshal(String xml) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Car.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
Car car = (Car) jaxbUnmarshaller.unmarshal(reader);
LOGGER.info(car.toString());
return car;
}
Next, pass below string representation of an XML to the above unmarshal()
method.
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" "
+ "standalone=\"yes\"?>"
+ "<ns2:Car xmlns:ns2=\"com.codenotfound.jaxb.model\" id=\"ABC-123\">"
+ "<make>Passat</make>"
+ "<manufacturer>Volkswagen</manufacturer></ns2:Car>";
JAXB is now able to unmarshal the StringReader
object and the result is the following:
Car [make=Passat, manufacturer=Volkswagen, id=ABC-123]
If you would like to run the above code sample you can get the full source code on GitHub.
This concludes our short example on how to unmarshal an XML String
. If you found this post helpful or have any questions or remarks, please leave a comment.