[英]XML XPath Parsing in Java
這是以下標准代碼,用於使用Java中的XPath解析XML。 我無法調試為什么我得到空值。 我已經附加了java文件,xml文件和輸出。 如果有人可以解釋我哪里出錯了,將不勝感激。 提前致謝! :)
XPathParser.java
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XPathParser {
public static void main(String args[]) throws Exception {
//loading the XML document from a file
DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
builderfactory.setNamespaceAware(true);
//XML read
DocumentBuilder builder = builderfactory.newDocumentBuilder();
Document xmlDocument = builder.parse("Stocks.xml");
// Creates a XPath factory
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
//Creates a XPath Object
XPath xPath = factory.newXPath();
//Compiles the XPath expression
//XPathExpression xPathExpression_count = xPath.compile("count(//stock)");
XPathExpression xPathExpression = xPath.compile("//stock");
//Run the query and get a nodeset
Object result = xPathExpression.evaluate(xmlDocument,XPathConstants.NODESET);
//Cast the result into a DOM nodelist
NodeList nodes = (NodeList) result;
System.out.println(nodes.getLength());
System.out.println(nodes.item(0));
for (int i=0; i<nodes.getLength();i++){
System.out.println(nodes.item(i).getNodeValue());
}
}
}
Stocks.xml
<?xml version="1.0" encoding="UTF-8"?>
<stocks>
<stock>
<symbol>ABC</symbol>
<price>10</price>
<quantity>50</quantity>
</stock>
<stock>
<symbol>XYZ</symbol>
<price>20</price>
<quantity>1000</quantity>
</stock>
</stocks>
輸出:
2
[stock: null]
null
null
您嘗試在Stock
節點上調用getNodeValue
方法-這沒有意義,因為它們沒有值,它們是父節點。
您可以遍歷Stock
的子節點並查找信息:
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
final NodeList childList = ((Element) nodeList.item(i)).getChildNodes();
for (int j = 0; j < childList.getLength(); ++j) {
final Node node = childList.item(j);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(node.getNodeName() + "=" + node.getTextContent());
}
}
}
輸出:
symbol=ABC
price=10
quantity=50
symbol=XYZ
price=20
quantity=1000
請注意,您必須按類型過濾子Node
,否則將遍歷子節點和該節點的文本值(作為文本節點出現)的組合。 這是以這種方式遍歷XML的常見陷阱。
您還可以遍歷Stock
所有子文本節點:
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock/*/text()");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
final Node node = nodeList.item(i);
System.out.println(node.getNodeValue());
}
輸出:
ABC
10
50
XYZ
20
1000
在這種情況下,您將遍歷Stock
子級的子級的所有文本節點-這意味着您會丟失有關節點名的信息。 但是您可以通過遍歷不是文本節點的所有Stock
子級來重新創建第一種方法:
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock/*");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
final Node node = nodeList.item(i);
System.out.println(node.getNodeName() + "=" + node.getTextContent());
}
輸出:
symbol=ABC
price=10
quantity=50
symbol=XYZ
price=20
quantity=1000
或者,如果需要更具體的信息,則可以在XPath中選擇一個特定的子節點:
final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock/symbol/text()");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
final Node node = nodeList.item(i);
System.out.println(node.getNodeValue());
}
輸出:
ABC
XYZ
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.