ノード値を取得するために xPath を使用しています。ここに私のxmlがあります
<?xml version="1.0" encoding="UTF-8"?>
<address>
<buildingnumber> 29 </buildingnumber>
<street> South Lasalle Street</street>
<city>Chicago</city>
<state>Illinois</state>
<zip>60603</zip>
</address>
これは私が訴えているコードです
DocumentBuilder builder = tryDom.getDocumentBuilder();
Document xmlDocument = tryDom.getXmlDocument(builder, file);
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression xPathExpression = null;
String expression7 = "//address/descendant-or-self::*";
try {
xPathExpression = xPath.compile(expression7);
Object result = xPathExpression.evaluate(xmlDocument,XPathConstants.NODESET);
printXpathResult(result);
} catch (XPathExpressionException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
public static void printXpathResult(Object result){
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
String nodeName = node.getNodeName();
String nodeValue = node.getNodeValue();
System.out.println(nodeName + " = " + nodeValue);
}
} //end of printXpathResult()
私が得ている出力は
address = null
buildingnumber = null
street = null
city = null
state = null
zip = null
私はこの出力を期待しています
address = null
buildingnumber = 29
street = South Lasalle Street
city = Chicago
state = Illinois
zip = 60603
buildingnumber と other に値があるのに null になるのはなぜですか? どうすれば目的の出力を取得できますか?
ありがとう
編集 - - - - - - - - - - - - - - - - - - - - - - - - - -------------
public static void printXpathResult(Object result){
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
String nodeName = node.getNodeName();
String nodeValue = node.getTextContent();
System.out.println(nodeName + " = " + nodeValue);
}
} //end of printXpathResult()
この後、次の出力が得られます
address =
29
South Lasalle Street
Chicago
Illinois
60603
buildingnumber = 29
street = South Lasalle Street
city = Chicago
state = Illinois
zip = 60603
アドレス = 29 を取得している理由 .... . 私はそれがすべきだと思いますaddress = null
か?
ありがとう