gpt4 book ai didi

java - Xpath递归获取值

转载 作者:行者123 更新时间:2023-12-02 06:06:36 25 4
gpt4 key购买 nike

我有一个像这样的 xml 文档

<?xml version="1.0"?>
<Employees>
<Employee emplid="1111" type="admin">
<firstname>John</firstname>
<lastname>Watson</lastname>
<age>30</age>
<email>johnwatson@sh.com</email>
<Employee emplid="2222" type="Manager">
<firstname>Sherlock</firstname>
<lastname>Homes</lastname>
<age>32</age>
<email>sherlock@sh.com</email>
<city>
<name>ger</name>
</city>
<Employee emplid="3333" type="Clerk">
<firstname>Tom</firstname>
<lastname>Irish</lastname>
<age>32</age>
<email>tom@sh.com</email>
<city>
<name>tok</name>
</city>
<Employee emplid="3333" type="Staff">
<firstname>Jerking</firstname>
<lastname>rash</lastname>
<age>32</age>
<email>jer@sh.com</email>
<city>
<name>rus</name>
</city>
</Employee>
</Employee>
</Employee>
<city>
<name>cal</name>
</city>
</Employee>
</Employees>

我想得到如下所示的输出

empid = admin
firstname = john
last name = watson
age = 30
city name = cal

empid = manager
firstname = sherlock
last name = homes
age = 32
city name = ger

empid = clerk
firstname = tom
last name = irish
age = 32
city name = tok

empid = staff
firstname = jerking
last name = rash
age = 30
city name = rus

我已经尝试过,但没有以正确的顺序获取值..

到目前为止我已经尝试过了

public static void main(String[] args) throws Exception {
DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = Factory.newDocumentBuilder();
FileInputStream file = new FileInputStream(new File("D:/new11.xml"));
Document doc = builder.parse(file);

//creating an XPathFactory:
XPathFactory factory = XPathFactory.newInstance();
//using this factory to create an XPath object:
XPath xpath = factory.newXPath();

// XPath Query for showing all nodes valuetext()
XPathExpression expr = xpath.compile("//" + "Employee" + "/*");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
System.out.println(nodes.getLength());
for (int i = 0; i < nodes.getLength(); i++) {

Element el = (Element) nodes.item(i);

System.out.println("tag: " + el.getNodeName());
// seach for the Text children
if (el.getFirstChild().getNodeType() == Node.TEXT_NODE){

System.out.println("inner value:" + el.getFirstChild().getNodeValue());
//System.out.println("inner value:" + el.getNodeName());
}


NodeList children = el.getChildNodes();
for (int k = 0; k < children.getLength(); k++) {
Node child = children.item(k);
if (child.getNodeType() != Node.TEXT_NODE) {
System.out.println("child tag: " + child.getNodeName());
if (child.getFirstChild().getNodeType() == Node.TEXT_NODE)
System.out.println("inner child value:" + child.getFirstChild().getNodeValue());;
}
}
}
}

最佳答案

子元素的典型递归的 XPath 为:

  • 选择 child 的 child 的 child 的任何 child 等等......

    后代或 self ::*

  • 选择特定节点名称

    后代或 self ::节点名称

例如descendant-or-self::foo 将选择当前的所有后代名为 foo 的上下文。

要将其放入递归循环中,您可以使用以下命令:

<xsl:for-each select="document/node">
<xsl:for-each select="descendant-or-self::*>
...
</xsl:for-each>
</xsl:for-each>

了解更多信息:http://www.stylusstudio.com/xsllist/200310/post30960.html

关于java - Xpath递归获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22243697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com