http://puu.sh/52XYZ/6533d7013f.png
上面是我试图通过Java解析的XML。
我无法指定元素的属性。
例如,latitude
和longitude
是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();
}
答案 0 :(得分:0)
String latitude = eElement.getElementsByTagName("point").item(0).getAttributes().getNamedItem("latitude").toString();
您也可以使用XPATH访问该元素。
//data/location/point/@latitude
如果您需要帮助,请告诉我们
是你在找什么,或者我不明白你的问题?