提问人:upog 提问时间:7/20/2022 最后编辑:kjhughesupog 更新时间:7/20/2022 访问量:242
XPath 选择多个子值
XPath to select multiple child values
问:
如何选择满足以下条件的所有/多个值
XPATH的
/X/X1/X2[@code='b']/X3/value
XML格式:
<X>
<X1>
<X2 code="a">
<X3>
<value>x3a1</value>
</X3>
</X2>
</X1>
<X1>
<X2 code="b">
<X3>
<value>x3b11</value>
</X3>
</X2>
</X1>
<X1>
<X2 code="b">
<X3>
<value>X3b12</value>
</X3>
</X2>
</X1>
</X>
法典:
import org.dom4j.Document;
import org.dom4j.Node;
Document doc = reader.read(new StringReader(xml));
Node valueNode = doc.selectSingleNode(XPATH);
期望值
x3b11, X3b12
答:
1赞
kjhughes
7/20/2022
#1
使用 Document.selectNodes()
选择多个节点,而不是 Document.selectSingleNode()。
还可以考虑 XPath.selectNodes()
;以下是 DOM4J Cookbook 中的完整示例:
import java.util.Iterator;
import org.dom4j.Documet;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
public class DeployFileLoaderSample {
private org.dom4j.Document doc;
private org.dom4j.Element root;
public void browseRootChildren() {
/*
Let's look how many "James" are in our XML Document an iterate them
( Yes, there are three James in this project ;) )
*/
XPath xpathSelector = DocumentHelper.createXPath("/people/person[@name='James']");
List results = xpathSelector.selectNodes(doc);
for ( Iterator iter = results.iterator(); iter.hasNext(); ) {
Element element = (Element) iter.next();
System.out.println(element.getName());
}
// select all children of address element having person element with attribute and value "Toby" as parent
String address = doc.valueOf( "//person[@name='Toby']/address" );
// Bob's hobby
String hobby = doc.valueOf( "//person[@name='Bob']/hobby/@name" );
// the second person living in UK
String name = doc.value( "/people[@country='UK']/person[2]" );
// select people elements which have location attriute with the value "London"
Number count = doc.numberValueOf( "//people[@location='London']" );
}
}
评论
Document.selectNodes()
Document.selectSingleNode()