【问题标题】:Retrieve xml node value with xpath使用 xpath 检索 xml 节点值
【发布时间】:2014-07-08 05:59:40
【问题描述】:

这是我的 xml 文件:bookstore.xml,我想要做的是我提供 xpath 作为输入,它应该给我它的节点值。但它给了我空值。

    <?xml version="1.0" encoding="UTF-8"?>
<bookstore>
  <book category="cooking">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="children">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="web">
    <title lang="en">XQuery Kick Start</title>
    <author>James McGovern</author>
    <author>Per Bothner</author>
    <author>Kurt Cagle</author>
    <author>James Linn</author>
    <author>Vaidyanathan Nagarajan</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
  <book category="web" cover="paperback">
    <title lang="en">Learning XML</title>
    <author>Erik T. Ray</author>
    <year>2003</year>
    <price>39.95</price>
  </book>
</bookstore>

这里是java代码:

public class XmlParserUsingXpath {


        public void xmlParser(XPathExpression xpathexp) throws ParserConfigurationException, XPathExpressionException{


            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

            DocumentBuilder dbuilder = factory.newDocumentBuilder();

            Document doc=null;

            try {
                 doc= dbuilder.parse(new FileInputStream("D:\\Bookstore.xml"));

            } catch (SAXException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();
            }

            doc.getDocumentElement().normalize();

            Object result = xpathexp.evaluate( doc, XPathConstants.NODESET );

            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                 System.out.println(nodes.item(i));
            }

        public static void main(String[] args){

            XmlParserUsingXpath o1= new XmlParserUsingXpath();

            XPath x=XPathFactory.newInstance().newXPath();

            System.out.println("Enter the Xpath Expression : ");

            Scanner sc= new Scanner(System.in);

            String scan= sc.nextLine();

            try {

                o1.xmlParser(x.compile(scan));

            } catch (XPathExpressionException | ParserConfigurationException e) {

                e.printStackTrace();
            }

        }



}

现在当我提供“//book”作为输入时,它给了我

[book: null]
[book: null]
[book: null]
[book: null]

"/bookstore/book[3]/author" 会给出

[author: null]
[author: null]
[author: null]
[author: null]
[author: null]

【问题讨论】:

    标签: java xml xpath xml-parsing


    【解决方案1】:

    变化:

            Object result = xpathexp.evaluate( doc, XPathConstants.NODESET );
    

            Object result = xpathexp.evaluate( doc, XPathConstants.NODE );
    

    【讨论】:

    • 它适用于以下 xpath /bookstore/book[3]/author[2] 输出:[#text: Per Bothner] 但是,像这样的 xpath //book 输出是:[#text : ] [title: null] [#text: ] [author: null] [#text: ] [year: null] [#text: ] [price: null] [#text: ]
    • @Vishal_Kotecha 你不能在一个循环中得到它。您必须添加更多循环来获取 childNotes。
    • 好的。万分感谢!还有一件事,这种程序是否有任何通用代码可以为给定的 xpath 提供输出(例如使用 xereces)?我对这件事很陌生。
    • @Vishal_Kotecha Look here 希望对您有所帮助。
    【解决方案2】:

    我只是斜着看你的帖子,但试试:

    factory.setNamespaceAware(false);
    

    【讨论】:

    • 它不起作用:(你能详细说明是什么导致输出为空吗?
    • XPath 默认情况下(据我所知)是命名空间感知的。不要使用可能导致空值的命名空间。
    猜你喜欢
    • 1970-01-01
    • 2011-05-30
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多