나는 웹을 통해 검색하고 많은 블로그, 포럼 및 웹 사이트를 통해 해결책을 찾고자 노력해 왔으며 여전히이 문제에 봉착하고 있습니다. 나는 재귀 방법으로이 XML 파일을 구문 분석하기 위해 노력하고있어 자바, 그리고XML 재귀 적 노드 파서
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<level_one>
<level_two>
#text
</level_two>
<level_two>
#text
</level_two>
</level_one>
</root>
:
나는이 같은 XML 소스를 가지고있다. 이 제 기능입니다 : 일단
public void parseXML(Node root, Node parent)
{
if (root.hasChildNodes())
{
NodeList childrens = root.getChildNodes();
for (int i = 0; childrens.getLength(); i++)
{
parseXML(childrens.item(i), root);
}//for
}//fi:root_childrens
else
System.println.out(parent.getNodeName()+"::"+root.getNodeValue());
}
, 나는 XML 파일을로드 한, 내가 할 :
xmlDoc.normalize();
parseXML(xmlDoc.getFirstChild(), null);
확인이 더 많거나 적은 ... 작동합니다. 이제 내가 갖는 응답은 다음과 같다 :
root:: //OK
level_one:: //OK
level_two::#text //OK
level_one:: //WRONG: should not appear any more
level_two::#text //OK
level_one:: //WRONG: should not appear any more
root:: //WRONG: should not appear any more
나는 다음과 같은 라이브러리를 사용하고 있습니다 : 당신의 도움에 대한
import javax.xml.*;
import org.w3c.dom.*;
감사합니다.
편집 1 : 참고하시기 바랍니다. XML 파일은 2 레벨보다 깊게갑니다.
편집 2 : 나는 parent.getNodeName() root.getNodeName()로 변경하면, 나는 다음과 같은 응답을 얻을 : 프로그램이 빈 텍스트 노드를 표시하기 때문에
#text::
#text::
#text::
...
미안 내 실수
가 원하는 출력을 획득하기 위해, 당신은 뭔가를 쓸 수 있습니다. –