英文:
Java XML Parser to return all tags at the same level
问题
你可以尝试使用XPath表达式 //document/tag
来获取直接嵌套在 <document>
内的 <tag>
元素。这个表达式只会匹配位于直接子级位置的 <tag>
元素,而不会匹配子元素中的 <tag>
元素。
以下是你的代码的修改版本:
public static void parseXML() {
File file = new File("src//data//data.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = null;
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
doc = documentBuilder.parse(file);
} catch (SAXException | IOException e) {
e.printStackTrace();
}
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("//document/tag"); // 修改XPath表达式
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(i);
System.out.println(nodeList.item(i).getTextContent());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
通过这个修改,XPath表达式 //document/tag
只会返回直接嵌套在 <document>
内的 <tag>
元素。
英文:
TLDR of what I'm trying to do:
-
Use an xml document to store representation of classes.
-
Use some parser class to recurse through the file and instantiate instances of those classes.
-
I am currently having issues with getting child elements (and not grandchildren) of the nodeList.
Thanks in Advance.
Suppose I have the following xml document
====== data.xml ======
<document>
<tag>
<child1> some content here </child1>
<child2> some other content </child2>
</tag>
<tag>
<child1> interesting content here </child1>
<child2>
<tag>
<child1> a b c d </child1>
<child2> e f g h </child2>
</tag>
</child2>
</tag>
</document>
I have tried using the xFile parser to get all tag nodes. However, when I do the following:
public static void parseXML() {
File file = new File("src//data//data.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = null;
try {
documentBuilder = documentBuilderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document doc = null;
try {
doc = documentBuilder.parse(file);
} catch (SAXException | IOException e) {
e.printStackTrace();
}
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expr = xPath.compile("//document//tag");
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(i);
System.out.println(nodeList.item(i).getTextContent());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
I get the following output:
0
some content here
some other content
1
interesting content here
a b c d
e f g h
2
a b c d
e f g h
How do I get it to ONLY return the <tag>
elements which are directly nested in <document>
(because the path provided to xPath.compile(String string);
is //document//tag
答案1
得分: 1
为了回答你提出的具体问题 - 我会将这部分代码更改为
XPathExpression expr = xPath.compile("/document/tag[1]/*");
// 匹配所有子节点,根据你的结构,你没有任何子节点。
英文:
To answer the specific question you have posed - I would change this
XPathExpression expr = xPath.compile("//document//tag");
to this
XPathExpression expr = xPath.compile("/document/tag[1]/*");
The // matches all sub nodes and according to your structure you don't have any.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论