Java XML解析xPath未获取所有子元素
我有一个包含很多元素的非常大的XML文件 我只对下面的例子感兴趣。xml文档中大约有400个案例,我想通过文档解析并打印出每个元素和名称Java XML解析xPath未获取所有子元素,java,xml,dom,xpath,Java,Xml,Dom,Xpath,我有一个包含很多元素的非常大的XML文件 我只对下面的例子感兴趣。xml文档中大约有400个案例,我想通过文档解析并打印出每个元素和名称 <cases> <case> <id/> <title/> <type/> <priority/> <estimate/> <references/> <custom> <f
<cases>
<case>
<id/>
<title/>
<type/>
<priority/>
<estimate/>
<references/>
<custom>
<functional_area/>
<technology_dependence/>
<reviewed/>
<steps_completed>
</steps_completed>
<preconds> </preconds>
<steps_seperated>
<step>
<index/>
<content>
</content>
<expected>
</expected>
</step>
<step>
<index/>
<content>
</content>
<expected>
</expected>
</step>
<step>
</steps_seperated>
</custom>
</case>
目前,我的代码工作正常,直到“steps_Separated”停止并进入下一个案例
我的代码如下所示(下面是MVCE)
我不明白为什么它在“步骤分离”后停止并开始一个新的案例
我注意到的第二个问题是它只显示10个左右的案例(我不确定这是否是因为我在netbeans中运行它)
非常感谢您的帮助,谢谢
p、
mvce
public void printCaseElements(节点列表){
对于(int i=0;我可以请你创建一个???嗨,对不起,我不知道什么是MVCE?请跟随链接…好的,给我一分钟,我将编辑我的帖子,显示一个接受节点列表的方法,一个应该编辑帖子的方法。它现在被设置为一个接受节点列表并输出元素的方法,但出于某种原因,它会被删除他被困在“分离的步骤”
public void printCaseElements(NodeList list){
for(int i = 0 ;i <list.getLength();i++){
Element el = (Element) list.item(i);
System.out.println("tag: " + el.getNodeName());
if(el.getFirstChild().getNodeType() == Node.TEXT_NODE)
{
System.out.println("Inner Value: "+ el.getFirstChild().getNodeValue());
System.out.println("________________________________________________________________________");
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());
System.out.println("____________________________________________________________________________________________");
}
}
}
}
}
DOMParser parser =new DOMParser() ;
InputSource source = new InputSource(path) ;
try {
parser.parse(source);
Element docElement = parser.getDocument().getDocumentElement();
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
XPathExpression expression =xPath.compile("//case/*");
NodeList list =(NodeList) expression.evaluate(docElement,XPathConstants.NODESET);
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document newDoc = documentBuilder.newDocument();
Element newElement = newDoc.createElement("cases");
newDoc.appendChild(newElement);
for(int i =0 ; i <list.getLength(); i++){
Node n = newDoc.importNode(list.item(i), true);
newElement.appendChild(n);
}