Convert xml string to Java object

I have the following xml line. I want to convert it to a java object in order to map each tag to the fields of this object. It is better if I can enter different field names compared to the tag name. How can i do this? I am watching JAXB, but I'm still confused about parts like "ns4: response" and tags in tags. Thanks in advance...

<ns4:response> <count>1</count> <limit>1</limit> <offset>1</offset> <ns3:payload xsi:type="productsPayload"> <products> <product> <avgRating xsi:nil="true"/> <brand>Candie's</brand> <description> <longDescription> long descriptions </longDescription> <shortDescription> short description </shortDescription> </description> <images> <image> <altText>alternate text</altText> <height>180.0</height> <url> url </url> <width>180.0</width> </image> </images> <price> <clearancePrice xsi:nil="true"/> <regularPrice xsi:nil="true"/> <salePrice>28.0</salePrice> </price> </product> </products> </ns3:payload> </ns4:response> 
+7
source share
4 answers

JAXB is the Java standard ( JSR-222 ) for converting objects to / from XML. The following should help:

Cancel sort from row

You will need to wrap String in an instance of StringReader before your JAXB impl can cancel it.

 StringReader sr = new StringReader(xmlString); JAXBContext jaxbContext = JAXBContext.newInstance(Response.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Response response = (Response) unmarshaller.unmarshal(sr); 

Different field names and XML

You can use the @XmlElement annotation to specify what the name of the element will be. By default, JAXB scans properties. If you want to create mappings in the fields, you need to install @XmlAccessorType(XmlAccessType.FIELD) .

 @XmlElement(name="count") private int size; 

Namespaces

The @XmlRootElement and @XmlElement also let you specify namespace qualifications where necessary.

 @XmlRootElement(namespace="http://www.example.com") public class Response { } 

Additional Information

+16
source

JAXB is a good shot. If you have an XSD file for this document, it will be very simple. JAXB can generate Java code for a specified schema. If you do not have an XSD file, you will need to prepare the Java classes yourself. Find the JAXB tutorial and check out the documentation at http://jaxb.java.net/tutorial/ . Tags in tags are only nested objects for JAXB. ns4 is the namespace. JAXB supports namespaces - just view it in the documentation. You can use annotations to enter different field names than tags in XML. Follwo documentation.

+2
source

You have an XSD for the above XML.
I would recommend you use Jaxb.
JAXB creates java objects from XML files. You will need to first generate Java classes using the jaxb code generator, which accepts XSD as input, and then serializes / deserializes these XML files accordingly.

0
source

If you already have xml and have several attributes, you can process it as follows:

  String output = "<ciudads><ciudad><idCiudad>1</idCiudad> <nomCiudad>BOGOTA</nomCiudad></ciudad><ciudad><idCiudad>6</idCiudad> <nomCiudad>Pereira</nomCiudad></ciudads>"; DocumentBuilder db = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); InputSource is = new InputSource(); is.setCharacterStream(new StringReader(output)); Document doc = db.parse(is); NodeList nodes = ((org.w3c.dom.Document) doc) .getElementsByTagName("ciudad"); for (int i = 0; i < nodes.getLength(); i++) { Ciudad ciudad = new Ciudad(); Element element = (Element) nodes.item(i); NodeList name = element.getElementsByTagName("idCiudad"); Element element2 = (Element) name.item(0); ciudad.setIdCiudad(Integer .valueOf(getCharacterDataFromElement(element2))); NodeList title = element.getElementsByTagName("nomCiudad"); element2 = (Element) title.item(0); ciudad.setNombre(getCharacterDataFromElement(element2)); ciudades.getPartnerAccount().add(ciudad); } } for (Ciudad ciudad1 : ciudades.getPartnerAccount()) { System.out.println(ciudad1.getIdCiudad()); System.out.println(ciudad1.getNombre()); } 

getCharacterDataFromElement method

  public static String getCharacterDataFromElement(Element e) { Node child = e.getFirstChild(); if (child instanceof CharacterData) { CharacterData cd = (CharacterData) child; return cd.getData(); } return ""; } 
0
source

All Articles