我有一个包含大量元素的非常大的XML文件。
我只对看起来像下面例子的情况感兴趣。 xml文档中有大约400个案例我要解析整个文档并打印出每个元素和名称。
<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_seperated”停止并进入下一个案例。
我的代码看起来像这样(MVCE BELOW)
我无法理解为什么它会在“steps_seperated”之后停止并开始一个新案例
我注意到的第二个问题是它只显示了10个左右的情况(我不确定这是因为我在netbeans中运行它)
非常感谢任何帮助,谢谢
P.S
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("____________________________________________________________________________________________");
}
}
}
}
}
P.P.S
我认为问题可能出在我使用xPath
的时候 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);
}
当我将(“// case / ”)更改为(“// steps_seperated / ”)时,它显示以下所有元素分开。但不是steps_seperated之前的元素