2013-05-27 111 views
-3

請幫我解析這個xml標籤,我想用類別ID解析這個xml標籤,就像下面給出的 ....?使用屬性的Android XML解析器

所需的輸出:

apple  100 iphone 5 500$ 
apple  200 iphone 4s 300$ 
Samsung 300 Galaxy 4 400$ 

XML樣本是在這裏:

<Items> 
    <category ID="99"> 
     <MobileDetails> 
     <MobileName>apple</MobileName> 
     <MobileID>100</MobileID> 
     <MobileModel>iphone 5</MobileModel> 
     <MobileCost>500$</MobileCost> 
     </MobileDetails> 
     <MobileDetails> 
     <MobileName>apple</MobileName> 
     <MobileID>200</MobileID> 
     <MobileModel>iphone 4s</MobileModel> 
     <MobileCost>300$</MobileCost> 
     </MobileDetails> 
    </category > 
    <category ID="100"> 
    <MobileDetails> 
     <MobileName>Samsung</MobileName> 
     <MobileID>300</MobileID> 
     <MobileModel>Galaxy 4</MobileModel> 
     <MobileCost>400$</MobileCost> 
    </MobileDetails> 
    </category > 
</Items> 
+0

那你希望得到儘可能輸出?並使用什麼編程語言/系統? –

+0

這是我的輸出,我希望此標籤解析並顯示在自定義列表視圖上。請幫我解析使用類別ID –

+0

列表視圖在什麼? ASP.NET? PHP? SQL Server?你的問題很不完整,不能這樣回答 - 你需要告訴我們你想做什麼! –

回答

1
try { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     Document doc = builder.parse(new InputSource(new StringReader(xml))); 
     Element root =(Element) doc.getDocumentElement(); 
     //category 
     NodeList categoryNList = root.getElementsByTagName("category"); 
     int categoryLength = categoryNList.getLength(); 
     for (int i = 0; i < categoryLength; i++) { 
      Element element = (Element)categoryNList.item(i); 
      //fetch ID 
      String categoryID = element.getAttribute("ID"); 
      NodeList mobileDetailsNList = element.getElementsByTagName("MobileDetails"); 
      //inner 
      int MobileDetailsLength = elemnt.getElementsByTagName("MobileName"); 
      for(int j==0;int < j <MobileDetailsLength ; j++) { 
      NodeList MobileDetailsNList = element.getElementsByTagName("MobileDetails"); 
      Element element = (Element)categoryNList.item(i); 
      String name = mobileDetailsNList.item(0).getFirstChild().getNodeValue() ; 
      //... ... ... 
      //... ... ... 

      } 

     } 

    } catch (Exception e) { 

    } 
} 
maybe it will help you. 
+0

感謝您的寶貴答案,我有另一個疑問,我只需要類別ID =「99」的值我該怎麼做。 –

+0

String categoryID = element.getAttribute(「ID」);這個值? –