Xpath以字符串形式读取标记和属性
我有以下xml内容,基本上我希望将ref中的所有内容都作为一个字符串读取,其中包含DOM4JAPI中的xpath 我想要Xpath以字符串形式读取标记和属性,xpath,dom4j,Xpath,Dom4j,我有以下xml内容,基本上我希望将ref中的所有内容都作为一个字符串读取,其中包含DOM4JAPI中的xpath 我想要 约翰 新闻 简 财务 输出为原始字符串,包括标记和属性 <citation type="book"> <name>John</name><title>News</title></citation> <citation type="journal"> <name>Jane<
约翰
新闻
简
财务
输出为原始字符串,包括标记和属性
<citation type="book"> <name>John</name><title>News</title></citation>
<citation type="journal"> <name>Jane</name><title>Finance</title></citation>
约翰尼斯
詹妮弗金融
我的代码是
org.dom4j.Document doc = DomReader.parse("test.xml");
List<Node> nodes = doc.selectNodes("/list/ref" );
for (Node node : nodes)
{
System.out.println( node.getStringValue());
}
org.dom4j.Document doc=DomReader.parse(“test.xml”);
列表节点=doc.selectNodes(“/List/ref”);
用于(节点:节点)
{
System.out.println(node.getStringValue());
}
上面的代码只给出了John、News、Jane和Finance的值
但是我希望包括全部内容。定义了.getStringValue()
方法来返回节点的XPath“string value”,对于一个元素来说,这意味着其所有子代文本节点的串联。对于节点的XML表示,您需要改用。我使用了node.asXML();它或多或少地给出了预期的结果。它给了约翰尼斯
org.dom4j.Document doc = DomReader.parse("test.xml");
List<Node> nodes = doc.selectNodes("/list/ref" );
for (Node node : nodes)
{
System.out.println( node.getStringValue());
}