0
我需要解析n級xml文件並顯示其元素。它永遠不會有任何屬性。 我當前的代碼如何使用DOM解析n級xml
String xmlInputFile="reportA.xml" ;
File file =new File(xmlInputFile);
Document document;
DocumentBuilder documentBuilder;
DocumentBuilderFactory documentBuilderFactory;
NodeList nodeList;
documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilder = documentBuilderFactory.newDocumentBuilder();
document = documentBuilder.parse(xmlInputFile);
document.getDocumentElement().normalize();
nodeList = document.getElementsByTagName("*");
for (int index = 0; index < nodeList.getLength(); index++) {
Node nodeA = nodeList.item(index);
if (nodeA.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) nodeA;
if(element.hasChildNodes()){
System.out.println("Name "+element.getNodeName()+" value "+element.getFirstChild().getNodeValue().trim());
}
}
}
我的XML文件是
<?xml version="1.0" encoding="UTF-8" ?>
<company>
<record>
<name>
<firstName>Brad</firstName>
<lastName>Pitt</lastName>
</name>
<age>41</age>
<dob>31/8/1982</dob>
<income>200,000</income>
</record>
</company>
目前放出來的是:
Name company value
Name record value
Name name value
Name firstName value Brad
Name lastName value Pitt
Name age value 41
Name dob value 31/8/1982
Name income value 200,000
我不需要公司,唱片,名字。如何刪除這些元素?