A.J
A.J

Reputation: 1180

Map nested XML into Java variables

I have the following XML example:

<DiagnosticResult>
   <RESULTSHEADER>
      <VEHICLE>
         <IDENT>
            <REGISTRATION>car registration</REGISTRATION>
            <MANUFACTURER>car manuf</MANUFACTURER>
            <MODEL>car model</MODEL>
         </IDENT>
         <DATA>
            <VEHICLE_IMAGE>veh url</VEHICLE_IMAGE>
         </DATA>
      </VEHICLE>
   </RESULTSHEADER>
   <RESULT OBJECT="WHEEL_ALIGNMENT" METHOD="MANUFACTURER_SPECIFIC">
      <SECTION OBJECT="TIRE_INSPECTION" AXLE="1">
         <TITLE>Tire Inspection Axle 1</TITLE>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">4.491</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.871</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">6.277</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">8/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.694</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">4.821</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">4.560</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_4" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.158</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
         </MEAS>
      </SECTION>
      <SECTION OBJECT="TIRE_INSPECTION" AXLE="2">
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.199</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">6.052</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">8/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="LEFT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">6.275</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">8/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.270</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_2" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.249</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
         <MEAS OBJECT="TREAD_DEPTH_DISPLAY_3" LOC="RIGHT">
            <TITLE>Tread Depth Point</TITLE>
            <VALUE UNIT="mm" RESULT="0">5.685</VALUE>
            <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">7/32</VALUE>
         </MEAS>
      </SECTION>
      <SUMMARY>
         <TITLE>Quick Check Results</TITLE>
         <MEAS OBJECT="TIRE_CHECK">
            <TITLE>Tread Depth</TITLE>
            <VALUE RESULT="1">PASSED</VALUE>
         </MEAS>
      </SUMMARY>
   </RESULT>
</DiagnosticResult>

There are more fields in between, but the provided snippet is what I am interested in. I would like to declare variables based on this XML.

I have no problem getting specific values just based on the tag. For an example, the registration.

My code is:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));

String registration = doc.getElementsByTagName("REGISTRATION").item(0).getTextContent();

The issue I am having is how to access the fields with identical names. for an example this one:

 <MEAS OBJECT="TREAD_DEPTH_DISPLAY_1" LOC="LEFT">
    <TITLE>Tread Depth Point</TITLE>
    <VALUE UNIT="mm" RESULT="0">4.491</VALUE>
    <VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/32" RESULT="1">6/32</VALUE>
 </MEAS>

It shows up four times in the file. Twice under <SECTION OBJECT="TIRE_INSPECTION" AXLE="1"> and twice under <SECTION OBJECT="TIRE_INSPECTION" AXLE="2">. I would need to have the data from the whole block. result, value, lowlim1, etc.

Is there a way for me to get these fields using the method I currently have, or do I need a different approach?

EDIT:

Current code: https://code.sololearn.com/c9RLoBhB7Mua

public class Program {


    public static String frontLeft1 = "";
    public static String frontLeft1Status = "";
    public static String frontLeft2 = "";
    public static String frontLeft2Status = "";

    public static String frontRight1 = "";
    public static String frontRight1Status = "";
    public static String frontRight2 = "";
    public static String frontRight2Status = "";

    public static String rearLeft1 = "";
    public static String rearLeft1Status = "";
    public static String rearLeft2 = "";
    public static String rearLeft2Status = "";

    public static String rearRight1 = "";
    public static String rearRight1Status = "";
    public static String rearRight2 = "";
    public static String rearRight2Status = "";


    public static void main(String[] args) {

        try {
            String xml = "<DiagnosticResult><REGISTRATION>123123</REGISTRATION><RESULT OBJECT=\"WHEEL_ALIGNMENT\" METHOD=\"MANUFACTURER_SPECIFIC\"><SECTION OBJECT=\"TIRE_INSPECTION\" AXLE=\"1\"><TITLE>Tire Inspection Axle 1</TITLE><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">1/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">2/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">3/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">4/32</VALUE></MEAS></SECTION><SECTION OBJECT=\"TIRE_INSPECTION\" AXLE=\"2\"><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">5.199</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">5/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"LEFT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"2/32\" RESULT=\"1\">6/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_1\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"4/32\" RESULT=\"1\">7/32</VALUE></MEAS><MEAS OBJECT=\"TREAD_DEPTH_DISPLAY_2\" LOC=\"RIGHT\"><TITLE>Tread Depth Point</TITLE><VALUE UNIT=\"mm\" RESULT=\"0\">4.491</VALUE><VALUE TYPE=\"DISP\" UNIT=\"finch\" LOWLIM1=\"2/32\" LOWLIM2=\"1/32\" RESULT=\"1\">8/32</VALUE></MEAS></SECTION></RESULT></DiagnosticResult>";
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.newDocument();
        document = dBuilder.parse(new ByteArrayInputStream(xml.getBytes()));

        NodeList nodeListMeas = document.getElementsByTagName("MEAS");
        NodeList nodeListSection = document.getElementsByTagName("SECTION");

        for (int i = 0; i < nodeListSection.getLength(); i++) {
            Element sectionElement = (Element) nodeListSection.item(i);
            if (sectionElement.getAttribute("AXLE").equals("1")) {
                for (int j = 0; j < nodeListMeas.getLength(); j++) {
                    Element measElement = (Element) nodeListMeas.item(j);
                    Node node = nodeListMeas.item(j);
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_1")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            frontLeft1 = childElement.getTextContent();
                            frontLeft1Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            frontRight1 = childElement.getTextContent();
                            frontRight1Status = childElement.getAttribute("RESULT");
                        }
                    }
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_2")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            frontLeft2 = childElement.getTextContent();
                            frontLeft2Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            frontRight2 = childElement.getTextContent();
                            frontRight2Status = childElement.getAttribute("RESULT");
                        }
                    }

                 }
            } else if (sectionElement.getAttribute("AXLE").equals("2")) {
                for (int j = 0; j < nodeListMeas.getLength(); j++) {
                    Element measElement = (Element) nodeListMeas.item(j);
                    Node node = nodeListMeas.item(j);
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_1")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            rearLeft1 = childElement.getTextContent();
                            rearLeft1Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            rearRight1 = childElement.getTextContent();
                            rearRight1Status = childElement.getAttribute("RESULT");
                        }
                    }
                    if (measElement.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_2")) {
                        NodeList childNodeList = measElement.getElementsByTagName("VALUE");
                        Element childElement = (Element) childNodeList.item(1);
                        if (measElement.getAttribute("LOC").equals("LEFT")) {
                            rearLeft2 = childElement.getTextContent();
                            rearLeft2Status = childElement.getAttribute("RESULT");
                        } else if (measElement.getAttribute("LOC").equals("RIGHT")) {
                            rearRight2 = childElement.getTextContent();
                            rearRight2Status = childElement.getAttribute("RESULT");
                        }
                    }

                }
            }
        }

        System.out.println(frontLeft1);
        System.out.println(frontLeft2);
        System.out.println(frontRight1);
        System.out.println(frontRight2);
        System.out.println(rearLeft1);
        System.out.println(rearLeft2);
        System.out.println(rearRight1);
        System.out.println(rearRight2);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This takes care of finding the elements, but it overwrites the variables. Output should be:

1/32
2/32
3/32
4/32
5/32
6/32
7/32
8/32

But right now it is:

5/32
6/32
7/32
8/32
5/32
6/32
7/32
8/32

Upvotes: 0

Views: 207

Answers (1)

Lakshan
Lakshan

Reputation: 1436

try with following solution,

public static void main(String[] args) {

  try {
     File inputFile = new File("Input.xml");
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
     Document document = dBuilder.parse(inputFile);
     document.getDocumentElement().normalize();
     NodeList nodeList = document.getElementsByTagName("MEAS");

     for (int i = 0; i < nodeList.getLength(); i++) {
         Element element = (Element) nodeList.item(i);
         Node node = nodeList.item(i);
         if(element.getAttribute("OBJECT").equals("TREAD_DEPTH_DISPLAY_1")){
             NodeList childNodeList = element.getElementsByTagName("VALUE");
             for (int j = 0; j < childNodeList.getLength(); j++) {
                 Element childElement = (Element) childNodeList.item(j);
                 if(j==0){
                     System.out.println("First Value Element");
                     System.out.println("UNIT : "+childElement.getAttribute("UNIT")+
                             ", RESULT : "+childElement.getAttribute("RESULT")+
                             ", VALUE : "+childElement.getTextContent());
                 }else{
                     System.out.println("Second Value Element");
                     System.out.println("TYPE : "+childElement.getAttribute("TYPE")+
                             " UNIT : "+childElement.getAttribute("UNIT")+
                             " LOWLIM1 : "+childElement.getAttribute("LOWLIM1")+
                             " LOWLIM2 : "+childElement.getAttribute("LOWLIM2")+
                             ", RESULT : "+childElement.getAttribute("RESULT")+
                             ", VALUE : "+childElement.getTextContent());
                 }
            }
         }
     }
  } catch (Exception e) {
     e.printStackTrace();
  }
}

output,

First Value Element
UNIT : mm, RESULT : 0, VALUE : 4.491
Second Value Element
TYPE : DISP UNIT : finch LOWLIM1 : 2/32 LOWLIM2 : 4/32, RESULT : 1, VALUE : 6/32
First Value Element
UNIT : mm, RESULT : 0, VALUE : 5.694
Second Value Element
TYPE : DISP UNIT : finch LOWLIM1 : 2/32 LOWLIM2 : 4/32, RESULT : 1, VALUE : 7/32
First Value Element
UNIT : mm, RESULT : 0, VALUE : 5.199
Second Value Element
TYPE : DISP UNIT : finch LOWLIM1 : 2/32 LOWLIM2 : 4/32, RESULT : 1, VALUE : 7/32
First Value Element
UNIT : mm, RESULT : 0, VALUE : 5.270
Second Value Element
TYPE : DISP UNIT : finch LOWLIM1 : 2/32 LOWLIM2 : 4/32, RESULT : 1, VALUE : 7/32

Upvotes: 1

Related Questions