http://puu.sh/52XYZ/6533d7013f.png
以上是我试图通过 Java 解析的 XML。
我无法指定元素的属性。
例如,latitude
andlongitude
是 的属性,对point
吗?我无法具体说明。
这是我到目前为止所拥有的:
try {
File newXMLFile = new File("H:\\520\\SOAP Data Acquisition\\testNOAA.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(newXMLFile);
doc.getDocumentElement().normalize();
System.out.println("Title: " + doc.getElementsByTagName("title").item(0).getTextContent());
System.out.println("Field: " + doc.getElementsByTagName("field").item(0).getTextContent());
System.out.println("category: " + doc.getElementsByTagName("category").item(0).getTextContent());
System.out.println("Date Created: " + doc.getElementsByTagName("creation-date").item(0).getTextContent());
NodeList nList = doc.getElementsByTagName("data");
for(int i = 0; i < nList.getLength(); i++){
Node nNode = nList.item(i);
System.out.println("\nCurrent Element: " + nNode.getNodeName());
if(nNode.getNodeType() == Node.ELEMENT_NODE){
Element eElement = (Element) nNode;
// System.out.println("Location: " + eElement.getElementsByTagName("location").getElementsByTagName("point").getAttribute("latitude"));
System.out.println("Time: " + eElement.getElementsByTagName("time-layout").item(0).getTextContent()); // get first child
System.out.println("Start Time: " + eElement.getElementsByTagName("start-valid-time").item(0).getTextContent());
System.out.println("End Time: " + eElement.getElementsByTagName("end-valid-time").item(0).getTextContent());
System.out.println("Type: "+eElement.getAttribute(""));
System.out.println(eElement.getElementsByTagName("name").item(0).getTextContent() + ": " + eElement.getElementsByTagName("value").item(0).getTextContent());
System.out.println(eElement.getElementsByTagName("name").item(1).getTextContent() + ": " + eElement.getElementsByTagName("value").item(1).getTextContent());
}
}
}catch(Exception e){
e.printStackTrace();
}