遍历 NodeList Java
Looping over NodeList Java
我有以下代码:
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();
}
}
}
我遇到的问题是变量被覆盖了。
输出应该是:
1/32
2/32
3/32
4/32
5/32
6/32
7/32
8/32
但现在是:
5/32
6/32
7/32
8/32
5/32
6/32
7/32
8/32
我显然在循环中遗漏了一些东西,但我无法弄清楚到底是什么。
我正在遍历 AXLE 1
,然后我想我也在 AXLE 2
上做同样的事情,但看起来 AXLE 2
覆盖了 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>
您检索了整个文档的 "MEAS" 个标签。
NodeList nodeListMeas = document.getElementsByTagName("MEAS");
您可以移动并编辑此行以将搜索限制为一次只搜索一个部分:
for (int i = 0; i < nodeListSection.getLength(); i++) {
Element sectionElement = (Element) nodeListSection.item(i);
NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");
最终,问题是需要在每个循环内而不是在第 33 行之前的行上执行以下操作 (NodeList nodeListSection = document.getElementsByTagName("SECTION");)
NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");
此外,请注意,我仍在获取有问题的 NodeList,但我是从相应的 'sectionElement' 获取它的。
我有以下代码:
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();
}
}
}
我遇到的问题是变量被覆盖了。
输出应该是:
1/32
2/32
3/32
4/32
5/32
6/32
7/32
8/32
但现在是:
5/32
6/32
7/32
8/32
5/32
6/32
7/32
8/32
我显然在循环中遗漏了一些东西,但我无法弄清楚到底是什么。
我正在遍历 AXLE 1
,然后我想我也在 AXLE 2
上做同样的事情,但看起来 AXLE 2
覆盖了 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>
您检索了整个文档的 "MEAS" 个标签。
NodeList nodeListMeas = document.getElementsByTagName("MEAS");
您可以移动并编辑此行以将搜索限制为一次只搜索一个部分:
for (int i = 0; i < nodeListSection.getLength(); i++) {
Element sectionElement = (Element) nodeListSection.item(i);
NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");
最终,问题是需要在每个循环内而不是在第 33 行之前的行上执行以下操作 (NodeList nodeListSection = document.getElementsByTagName("SECTION");)
NodeList nodeListMeas = sectionElement.getElementsByTagName("MEAS");
此外,请注意,我仍在获取有问题的 NodeList,但我是从相应的 'sectionElement' 获取它的。