Reputation: 1180
I have the following code:
https://code.sololearn.com/c9RLoBhB7Mua
public class Program {
public static String frontLeft1 = "";
public static String frontLeft2 = "";
public static String frontRight1 = "";
public static String frontRight2 = "";
public static String rearLeft1 = "";
public static String rearLeft2 = "";
public static String rearRight1 = "";
public static String rearRight2 = "";
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();
} else if (measElement.getAttribute("LOC").equals("RIGHT")) {
frontRight1 = childElement.getTextContent();
}
}
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();
} else if (measElement.getAttribute("LOC").equals("RIGHT")) {
frontRight2 = childElement.getTextContent();
}
}
}
} 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();
} else if (measElement.getAttribute("LOC").equals("RIGHT")) {
rearRight1 = childElement.getTextContent();
}
}
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();
} else if (measElement.getAttribute("LOC").equals("RIGHT")) {
rearRight2 = childElement.getTextContent();
}
}
}
}
}
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();
}
}
}
The issue I am having is that the variables are being overwritten.
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
I am clearly missing something in the loop, but I can't figure out what is exactly.
I am iterating over AXLE 1
and then I think I am doing the same over AXLE 2
, but it looks like AXLE 2
writes over AXLE 1
.
XML:
<DiagnosticResult>
<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">5.871</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">5.694</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.821</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">6.052</VALUE>
<VALUE TYPE="DISP" UNIT="finch" LOWLIM1="2/32" LOWLIM2="4/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">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">8/32</VALUE>
</MEAS>
</SECTION>
</DiagnosticResult>
Upvotes: 0
Views: 1915
Reputation: 361
Ultimately, the issue is that one needs to do the following instead, within each loop, rather than on the line before line 33 (NodeList nodeListSection = document.getElementsByTagName("SECTION");)
NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");
Also, please note that I'm still getting the NodeList in question, but am getting it from the corresponding 'sectionElement' instead.
Upvotes: 1
Reputation: 183
You retrieve "MEAS" tags for the whole document.
NodeList nodeListMeas = document.getElementsByTagName("MEAS");
You can move this line and edit it to limit the search to one section at a time instead :
for (int i = 0; i < nodeListSection.getLength(); i++) {
Element sectionElement = (Element) nodeListSection.item(i);
NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");
Upvotes: 2