Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML TO JAVA POJO in efficient way

How to parse and create java pojo for below xml in an efficient way? Kindly suggest any efficient parser.

XML format is

<?xml version="1.0" encoding="utf-8"?>
<CCMainRootTag ID="12">
  <Header TableName="TableName"    TableVersion="12" TableID="12" CreatedDate="2013-02-09T15:35:33" CreatedByUserName="ABC" CreatedBySystem="ABC" />
  <ClassPrimary ID="12" Code="Y" DescriptionDK="DK language " DescriptionUK="" DefDK="" DefUK="" IFDGUID="">
    <ObjectClass ID="12" Code="YA" DescriptionDK="DK Language" DescriptionUK="" DefDK=""     DefUK="" IFDGUID="">
      <Synonym>
        <Concept Description="Description" Language="DK" />
        <Concept Description="" Language="UK" />
        <Concept Description="Description" Language="DK" />
        <Concept Description="" Language="UK" />
        <Concept Description="Description" Language="DK" />
        <Concept Description="" Language="UK" />
        <Concept Description="Description" Language="DK" />
        <Concept Description="" Language="UK" />
      </Synonym>
    </ObjectClass>
    <ObjectClass ID="12" Code="YB" DescriptionDK="DK Language" DescriptionUK="" DefDK="" DefUK="" IFDGUID=""> </ObjectClass>
    <ObjectClass ID="12" Code="YC" DescriptionDK="DK Language" DescriptionUK="" DefDK="" DefUK="" IFDGUID=""> </ObjectClass>
    <ObjectClass ID="12" Code="YD" DescriptionDK="DK language" DescriptionUK="" DefDK="" DefUK="" IFDGUID=""> </ObjectClass>
  </ClassPrimary>
</CCMainRootTag>

I already use thisLink but it have slow performance and having problem did't valid pojo.

I want to parser which provide me direct java pojo in a efficient way.

like image 272
DroidEngineer Avatar asked Feb 09 '13 14:02

DroidEngineer


People also ask

What is the best way to parse XML in Java?

DOM Parser is the easiest java xml parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes more memory.

Which XML parser is faster?

DOM Parser is faster than SAX Parser. Best for the larger sizes of files. Best for the smaller size of files. It is suitable for making XML files in Java.

How do you Unmarshal XML string to Java object using JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.

How do I read an XML string in Java?

You can parse the XML file using XPath expression, you can do XSL transformation, XSD schema validation and you can even parse whole XML file as String in just a couple of lines of code.


1 Answers

For those looking for JAXB code to convert xml to java object:

//Convert xml to String first
Element partyLoaderRequest; // your xml data
String xmlString = new XMLOutputter().outputString(partyLoaderRequest);   
InputStream is = new ByteArrayInputStream(xmlString.getBytes());
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = docBuilder.parse(is);
org.w3c.dom.Element varElement = document.getDocumentElement();
JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<Person> loader = unmarshaller.unmarshal(varElement, Person.class);
Person inputFromXml = loader.getValue();

whereas Person has proper XML annotations:

@XmlRootElement(name="Person")
public class CimbWlAdminUserAmendInput {
    @XmlElement(name="companyName",required=true,nillable=false) 
    private String companyName;
    ...
    //setters getters
    @XmlTransient
    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
}
like image 191
mel3kings Avatar answered Oct 27 '22 00:10

mel3kings