作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个像这样的 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/
我是一名优秀的程序员,十分优秀!