2013-07-20 33 views
0

問題是爲什麼我無法從rootElement中找到任何屬性?通過遞歸方法在xml中打印所有屬性

我的XML是

<?xml version="1.0" encoding="GBK"?> 

<AccountInfos> 
    <!--this is a test for dom4j--> 
    <AccountInfo1 WebsiteName="ÐÂÀË" Account="123">Account1</AccountInfo1> 
    <AccountInfo2 WebsiteName="ÍøÒ×" Account="123">Account2</AccountInfo2> 
</AccountInfos> 

,我的代碼是這樣的

private void treeWalker(Element element) 
{ 
    int size = element.nodeCount(); 
    for (int i = 0; i < size; i++) 
    { 
     Node node = element.node(i); 
     if (node instanceof Element) 
     { 
      treeWalker((Element) node); 
     } 
     else if(node instanceof Attribute) 
     { 
      Attribute attribute=(Attribute)node; 
      System.out.println(attribute.getName()+":"+attribute.getValue()); 
     } 
     else 
     { 
      continue; 
     } 
    } 
} 

,當我在這個方法調試我不能進入第二個if塊

回答

0

屬性是不視爲元素(或分支,實際)內容的一部分(如元素,註釋或文本節點)。您必須專門檢索,例如與一個attributeIterator()。

+0

非常感謝 – Marc