0

I have the following xml

<Company>
  <Company1>
    <Dept>
       <Name>M1</Name>
       <Employers>10</Employers>
       <Product>soap</Product>
       <Building>001</Building>
       <Compulsory>Yes</Compulsory>
    </Dept>
    <Dept>
       <Name>M2</Name>
          <Sub-Name>M2-01</Sub-Name>
             <Id>m1001</Id>
       <Employers>12</Employers>
       <Product>soap-cover</Product>
    </Dept>
  </Company1>
  <OtherDetails>
    <DeptOther>
       <Name>M3</Name>
       <Employers>10</Employers>
       <Product>soap-colour</Product>
       <Building>001</Building>
          <Sub>001-01</Sub>
       <Compulsory>Yes</Compulsory>
    </DeptOther>
  </OtherDetails>
</Company>

I need to read this xml and map each of these element to following POJOs.

Object1 - 'Company' which has attributes 'Company1' and 'OtherDetails'

Object2 - 'Company1' which has attributes 'Dept'

Object3 - 'Dept' which has attributes 'Name', 'Employers' etc.

I'm using org.apache.axiom.om.impl.builder.StAXOMBuilder in order to build the DocumentElement.

I'm using following code,

public static void main(String[] args) throws IOException {
    mapXMLtoPOJO(FILE_LOCATION);

}

private static boolean mapXMLtoPOJO(String fileLocation) {
    File file = new File(fileLocation);
    if (file.exists()) {
        OMElement fileElement;
        try {
            InputStream xmlInputStream = new FileInputStream(file);
            fileElement = new StAXOMBuilder(xmlInputStream).getDocumentElement();
        } catch (Exception e) {
            log.error("Error while parsing XML file : " + file.getAbsolutePath());
            return false;
        }

        elementWriter(fileElement);

    } else {
        return false;
    }
    return true;

}

public static void elementWriter(OMElement fileElement){
    if(fileElement != null){
        Iterator iterator1 = fileElement.getChildElements();
        int i = 0;
        while (iterator1.hasNext()){
            OMElement pp = (OMElement) iterator1.next();
            log.info(" -------------- " + i);
            log.info(" -0-0-0-0-0-0- " + pp.getLocalName() + " ----- " + pp.getText());
            i++;
            elementWriter(pp);
        }
    }
}

which print each of the above elements with their values.

But I couldn't find a way to map each of these elements correctly into the java objects created for each major tag as mentioned above.

Should I be store these values in a hash map and then later put them into the created objects? Or what is the most optimum algorithm to do this?

Any idea on how I could do this would be appreciated.

Mad
  • 435
  • 2
  • 17
  • please have a look at my post :https://stackoverflow.com/questions/51536150/reading-an-xml-from-a-javafx-application?noredirect=1#comment90076639_51544479 , It is somewhat you want to achieve – dangi13 Jul 27 '18 at 14:42
  • Apparently you have used org.w3c.dom.Element but in here I have to use org.apache.axiom.om.impl.builder.StAXOMBuilder as mentioned in the question. – Mad Jul 27 '18 at 14:48

0 Answers0