JDOM을 사용하여 XML 파일을 구문 분석하고 모든 요소에서 일부 정보를 얻어야합니다.JDOM을 사용하여 XML 파싱을 자동화하는 방법
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element1>something</element1>
<element2>
<subelement21>moo</subelement21>
<subelement22>
<subelement221>toto</subelement221>
<subelement222>tata</subelement222>
</subelement22>
</element2>
</root>
그래서 요소 1의 경우 쉽습니다. 그러나 요소 2의 경우 나는 그의 자녀들을 지나쳐야하며, 자녀들이 있다면 그들도 지나가도록해야합니다.
public static void getInfos(Vector<String> files) {
Document document = null;
Element root = null;
SAXBuilder sxb = new SAXBuilder();
for (int i =0 ; i< files.size() ; i++)
{
System.out.println("n°" + i + " : " + files.elementAt(i));
try
{
document = sxb.build(files.elementAt(i));
root = document.getRootElement();
List<?> listElements = root.getChildren();
Iterator<?> it = listElements.iterator();
while(it.hasNext())
{
Element courant = (Element)it.next();
System.out.println(courant.getName());
if(courant.getChildren().size() > 0)
{
// here is the problem -> the element has a children
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
재귀 호출 또는 이와 비슷한 기능을 사용할 수 있도록이 경우 무엇을 제안합니까?
감사합니다.
당신이 혼합 된 내용으로 –
@OpenMind을 무엇을 의미합니까 : 혼합 내용은 " zxcvasdf을 쿼티"같은 것들을이 곳 두 개 이상의 요소 텍스트 노드를 가지고 있도록, HTML에서 보는 것과 같다. –
나는 내 XML 파일에 이런 것들을 많이 가지고있다. 그래서 나는 당신의 방법이 나를 위해 일하지 않을 것이라고 생각한다. –