그래서 여기에 몇 가지 XML 파싱을 배우려고합니다. 그 이유는 무엇이든간에 이유를 불문하고 "text()"각 쿼리 끝에, 그렇지 않으면 나 반환 된 null 값을 가져옵니다. 나는이 "text()"결말의 기능을 실제로 이해하지 못한다. 그러나 나는 그것이 필요하지 않다는 것을 안다. 왜 내가 그것을 생략 할 수 없는지 궁금하다. 도와주세요! 내 코드는 다음과 같습니다.XPath 쿼리는 "text()"가 끝나지 않으면 작동하지 않습니다
import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;
public class ParseClass
{
public static void main(String[] args)
throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException
{
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("C:\\Users\\Brandon\\Job\\XPath\\XPath_Sample_Stuff\\catalog.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/catalog/book[author='Thurman, Paula']/title/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++)
{
System.out.println(nodes.item(i).getNodeValue());
}
}
}
PS. 당신이 눈치 채지 못했을 경우에. 내 구문 분석을 위해 XPath와 DOM을 사용하고 있습니다.
하지만 여전히 요소를 얻는다면 왜 text()없이 null을 반환합니까? –