我想仅获取字符串标记中的数字。目前,Java程序认为字符串标记为空。提前感谢您的帮助!
以下是XML:
<?xml version="1.0"?>
<ArrayOfstring xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<string>6540321</string>
<string>6540322</string>
<string>6540323</string>
<string>6540324</string>
<string>6540325</string>
</ArrayOfstring>
这是我尝试过的内容
public static void toOrderListFromXML() throws ParserConfigurationException, SAXException, IOException {
File fXmlFile = new File("test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("string");
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("Order number: " + eElement.getNodeValue());
}
}
}
输出:
Root element :ArrayOfstring
Current Element :string
Order number: null
Current Element :string
Order number: null
Current Element :string
Order number: null
.......................
.......................
以下是我试图遵循的教程:http://www.java2s.com/Code/Java/XML/ParseanXMLstringUsingDOMandaStringReader.htm
这个教程介绍了如何使用DOM和StringReader解析XML字符串。