Below is a program written to parse the accompanying XML file.
Problem: There are two lane-IDs, but the output of my code keeps outputting only one. I expect the output to be:
Current Element: detector-report
Detector ID : I-74 NB from 12th Ave to 7th Ave
Status : operational
Lane-ID : 1
Lane - ID: 2
The current code still outputs 1 on the last line.
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class FinalTrial {
public static void main(String argv[]) {
try {
File fXmlFile = new File("workFile_09282014 22-10-34c.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("detector-report");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Detector ID : " + eElement.getElementsByTagName("detector-id").item(0).getTextContent());
System.out.println("Status : " + eElement.getElementsByTagName("status").item(0).getTextContent());
NodeList lanes = eElement.getElementsByTagName("lane");
System.out.println(lanes.getLength());
for (int j = 0; j<lanes.getLength(); j++) {
Element lane = (Element) lanes.item(0);
System.out.println("Lane-ID : " + lane.getElementsByTagName("lane-id").item(0).getTextContent());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Below is the accompanying XML file:
<?xml version="1.0" encoding="utf-8"?>
<trafficDetectorData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.dummy-temp-address" xmlns:lrms="http://www.dummy-lrms-address" xsi:schemaLocation="http://www.dummy-temp-address C:\temp\test\Detectors.xsd">
<detector-report>
<detector-id>I-74 NB from Ave of the Cities t</detector-id>
<status>operational</status>
<lane>
<lane-id>1</lane-id>
<count>1</count>
<volume>1</volume>
<occupancy>0</occupancy>
<speed>122</speed>
<classes>
<class>
<class-id>Small</class-id>
<count>1</count>
<volume>1</volume>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
<lane>
<lane-id>2</lane-id>
<occupancy>0</occupancy>
<speed>137</speed>
<classes>
<class>
<class-id>Small</class-id>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
</detector-report>
<detector-report>
<detector-id>I-74 NB from 12th Ave to 7th Ave</detector-id>
<status>operational</status>
<lane>
<lane-id>1</lane-id>
<count>3</count>
<volume>3</volume>
<occupancy>3</occupancy>
<speed>100</speed>
<classes>
<class>
<class-id>Small</class-id>
<count>3</count>
<volume>3</volume>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
<lane>
<lane-id>2</lane-id>
<count>4</count>
<volume>4</volume>
<occupancy>3</occupancy>
<speed>116</speed>
<classes>
<class>
<class-id>Small</class-id>
<count>4</count>
<volume>4</volume>
</class>
<class>
<class-id>Medium</class-id>
</class>
<class>
<class-id>Large</class-id>
</class>
</classes>
</lane>
</detector-report>
</trafficDetectorData>
I urge most developers to use established libraries for XML document handling, to avoid errors such as not properly escaping attribute values.
Java's built-in XML parsing library works very well (org.w3c.dom.Document, etc...).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.