XML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
<Employee type="permanent">
<Name>Ali</Name>
<Id>3674</Id>
<Age>34</Age>
</Employee>
<Employee type="contract">
<Name>Hasan</Name>
<Id>3675</Id>
<Age>25</Age>
</Employee>
<Employee type="permanent">
<Name>Ahmet</Name>
<Id>3676</Id>
<Age>28</Age>
</Employee>
</Personnel>
XML.java:
public class XML{
DocumentBuilderFactory dfk;
Document doc;
public void xmlParse(String xmlFile) throws
ParserConfigurationException, SAXException,
IOException {
dfk= DocumentBuilderFactory.newInstance();
doc= dfk.newDocumentBuilder().parse(new File(xmlFile));
xmlParse();
}
public void xmlParse(){
doc.getDocumentElement().normalize();
Element element = doc.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("Personnel");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
}
}
}
大家好; 我如何获得姓名、ID、年龄等。我尝试了更多次但没有工作我只显示空值。对不起我的英语不好.. (XML文件>正确<)