尝试对在内存中创建的 xml 文档而不是从文件创建的文档使用 xPath
Trying to use xPath on an xml document created in memory not from a file
我正在将数据导出到 xml 文件中。我正在遍历导出各种数据的数据库,需要检查 xml 文档以查看它在我导出数据时是否已经存在。
当我尝试使用 xPath 查询 xml 文档时,它 returns 为空。我是否必须保存 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();
}
}
}
您的查询返回 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 文件中。我正在遍历导出各种数据的数据库,需要检查 xml 文档以查看它在我导出数据时是否已经存在。
当我尝试使用 xPath 查询 xml 文档时,它 returns 为空。我是否必须保存 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();
}
}
}
您的查询返回 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());