How could I convert a XML Document to a Java Object (or Array)? I readed the XML like this:
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File("file.xml"));
doc.getDocumentElement().normalize();
Now I want that XML as Object (or Array) but how should I do this? Are there any methods or tutorials or classes out there to do that?
Use XStream.
Object to XML
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);
The resulting XML looks like this:
<person>
<firstname>Joe</firstname>
<lastname>Walnes</lastname>
<phone>
<code>123</code>
<number>1234-456</number>
</phone>
<fax>
<code>123</code>
<number>9999-999</number>
</fax>
</person>
XML to Object
Person newJoe = (Person)xstream.fromXML(xml);
Also see
Reference
Simple
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With