【问题标题】:Trying to use xPath on an xml document created in memory not from a file尝试在内存中创建的 xml 文档上使用 xPath,而不是从文件中
【发布时间】:2018-01-03 16:11:50
【问题描述】:

我正在将数据导出到 xml 文件中。我正在循环导出各种数据的数据库,需要检查 xml 文档以查看它是否已经存在,因为我正在导出数据。

当我尝试使用 xPath 查询 xml 文档时,它返回 null。我是否必须保存 xml 文件然后重新加载它才能使 xPath 工作?难道我做错了什么?下面是我的测试代码。注意,我尝试使用 xmlDoc.selectNodes 以及 xPath。还是没有爱

这是控制台输出

<?xml version="1.0" encoding="UTF-8"?>

<root>
  <authors>
    <author>
      <name>Steven Rieger</name>
      <location>Illinois</location>
    </author>
    <author>
      <name>Dylan Rieger</name>
      <location>Illinois</location>
    </author>
  </authors>
</root>
org.dom4j.tree.DefaultDocument@e26948 [Document: name null]


public class TestXML
{

    public static void writeXmlDoc( Document document ) throws IOException
    {

        OutputFormat format = OutputFormat.createPrettyPrint();
        // lets write to a file
        XMLWriter writer = new XMLWriter( new FileWriter( "c:\\temp\\output.xml" ), format );
        writer.write( document );
        writer.close();

        // Pretty print the document to System.out
        writer = new XMLWriter( System.out, format );
        writer.write( document );
    }

    public static void main( String[] args ) throws Exception
    {
        org.dom4j.Document xmlDoc;
        List<Node> authors = null;
        Element author = null;
        try
        {
            xmlDoc = DocumentHelper.createDocument();
            Element root = xmlDoc.addElement( "root" );
            Element authorsElement = root.addElement( "authors" );
            Element author1 = authorsElement.addElement( "author" );
            author1.addElement( "name" ).addText( "Steven Rieger" );
            author1.addElement( "location" ).addText( "Illinois" );

            Element author2 = authorsElement.addElement( "author" );
            author2.addElement( "name" ).addText( "Dylan Rieger" );
            author2.addElement( "location" ).addText( "Illinois" );



            writeXmlDoc( xmlDoc );

//          authors = xmlDoc.selectNodes( "/authors" );

             XPath xpathSelector = DocumentHelper.createXPath("/authors");
             authors = xpathSelector.selectNodes( xmlDoc );

            for (Iterator<Node> authorIterator = authors.iterator(); authorIterator.hasNext();)
            {
                author = (Element) authorIterator.next();
                System.out.println( "Author: " + author.attributeValue( "name" ) );
                System.out.println( "  Location: " + author.attributeValue( "location" ) );
                System.out.println( "  FullName: " + author.getText() );
            }

            System.out.println( xmlDoc.toString() );
        } catch ( Exception e )
        {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

    标签: java xpath dom4j


    【解决方案1】:

    您的查询返回 null,因为 authors 不是 xml 的根。您可以改用这个 xpath:

    XPath xpathSelector = DocumentHelper.createXPath("/root/authors/author");
    

    另一个问题是您首先将名称和状态存储为作者的子元素,但后来您尝试将它们作为属性读取。 见https://www.w3schools.com/xml/xml_dtd_el_vs_attr.asp 您可以尝试像这样在循环中阅读它们:

    System.out.println("Author: " + author.selectSingleNode("name").getText());
    System.out.println("  Location: " + author.selectSingleNode("location").getText());
    

    【讨论】:

    • 非常感谢您的回复。我不是一个大 xml 家伙,这是最有帮助的。我虽然有一种方法可以让所有作者都使用 xpath 而不必知道父级。
    • 所以我得到了代码,再次感谢您!我有最后一个问题。我正在尝试通过其中一个元素进行快速搜索,但没有任何成功。我尝试了各种查询字符串,但没有选择任何内容。你能看到我错过了什么吗? String xPathQueryStr = "/root/authors/author[@location='Wisconsin']";作者 = xmlDoc.selectNodes(xPathQueryStr); if(authors.isEmpty()) { XPath xpathSelector = DocumentHelper.createXPath( xPathQueryStr );作者 = xpathSelector.selectNodes(xmlDoc); } authors 总是空列表
    • 又是因为'@'选择属性,你应该使用没有它的xpath;看这里w3schools.com/xml/xpath_syntax.asp
    猜你喜欢
    • 1970-01-01
    • 2011-11-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多