There are several ways to achieve the conversion of an XML string to a Java object:
Example code:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(new InputSource(new StringReader(xmlString)));
Example code:
JAXBContext jaxbContext = JAXBContext.newInstance(MyClass.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); MyClass myObject = (MyClass) unmarshaller.unmarshal(new StringReader(xmlString));
Example code:
XStream xstream = new XStream(); MyClass myObject = (MyClass) xstream.fromXML(xmlString);
Note that these are just a few examples and there are other libraries available for XML parsing and serialization. The choice of library depends on the specific requirements of the project.
Asked: 2023-07-11 14:20:00 +0000
Seen: 10 times
Last updated: Jul 11 '23