2013-02-05 57 views
0

我想解析下面的XML文件使用Dom解析器。但我得到前三個標籤 (日期,早餐,午餐)。如何獲取所有日期,早餐和午餐標籤。Android解析所有標籤使用Dom解析器

-<Content> 
    <Date>2/4/2013</Date> 
    <Breakfast>WG Biscuit, Grits, sausage patty, fruit, juice, milk</Breakfast> 
    <Lunch>Chicken tenders with sauce, WG affle stick and syrup, carrots-MC, romaine garden salad, fruit, juice, milk</Lunch> 
    <Date>2/5/2013</Date> 
    <Breakfast>grilleed cheese sandich, grits, fruit, juice, milk</Breakfast> 
    <Lunch>meat sauce w/WG pasta, green beans, caesar salad, WW garlic toast, fruit, juice, milk</Lunch> 
    <Date>2/6/2013</Date> 
    <Breakfast>WG biscuit with chicken patty, fruit, juice, milk</Breakfast> 
    <Lunch>WG pizza, spinach salad, WKcorn, fruit, juice, milk</Lunch> 
    <Date>2/7/2013</Date> 
    <Breakfast>WG french toast sticks (4), sausage links, fruit, juice, milk</Breakfast> 
    <Lunch>salisbury steak, black eyed peas, creamed potatoes with gravy, greens-MC, spring mixed salad, WW cornbread, fruit, juice, milk</Lunch> 
    <Date>2/8/2013</Date> 
    <Breakfast>WG breakfast bagel, yogurt, fruit, juice, milk</Breakfast> 
    <Lunch>BBQ rib portion on WG bun, sweet potato fries or yams, romaine garden salad, fruit, juice, milk</Lunch> 
    <Date>2/11/2013</Date> 
    <Breakfast>Mardi Gras Holiday - No School</Breakfast> 
    <Lunch/> 
</Content> 

我用下面的代碼:

StringBuilder sb=new StringBuilder(arg0[0]); 
      String findlink=sb.toString(); 
      try{ 
        HttpClient client=new DefaultHttpClient(); 
        HttpGet request=new HttpGet(); 
        request.setURI(new URI(findlink)) ; 

        HttpResponse response=client.execute(request); 
        //et.setText("its working"); 
       DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 

       DocumentBuilder Builder=factory.newDocumentBuilder(); 
       dom=Builder.parse(response.getEntity().getContent()); 
       dom.getDocumentElement().normalize(); 
        nList5=dom.getElementsByTagName("Content"); 
        for(int temp=0;temp<nList5.getLength();temp++) 
        { 
         Node nNode=nList5.item(temp); 
         if(nNode.getNodeType()==Node.ELEMENT_NODE) 
         { 
            Element eElement=(Element)nNode; 
            String base1 =getTagValue("Date",eElement); 
            Date.add(base1); 
            String base2 =getTagValue("Breakfast",eElement); 
            Breakfast.add(base2); 
            String base3 =getTagValue("Lunch",eElement); 
            Lunch.add(base3); 


          } 
        } 

如何解析所有標籤在做這個下content.help我。

+0

我在這個XML看問題。三個標籤混在一起,需要用分隔標籤包裝。您不應只依賴訂單來分組相關標籤。 –

+0

第一個研究笏你需要做和..自己:) http://tutorials.jenkov.com/java-xml/dom.html – jenuine

回答

0

首先確保您的XML數據是格式化的我的意思是它不應該錯過任何標籤關閉和大括號<>使用任何在線XML formator來驗證您的XML。之後嘗試下面的代碼。

XMLParser的功能

/** 
* Getting XML DOM element 
* 
* @param XML 
*   string 
* */ 
public Document getDomElement(String xml) { 
    Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 

    dbf.setCoalescing(true); 
    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
     is.setCharacterStream(new StringReader(xml)); 
     doc = db.parse(is); 

    } catch (ParserConfigurationException e) { 
     Log.e("Error: ", e.getMessage()); 
     return null; 
    } catch (SAXException e) { 
     Log.e("Error: ", e.getMessage()); 
     return null; 
    } catch (IOException e) { 
     Log.e("Error: ", e.getMessage()); 
     return null; 
    } 

    return doc; 

} 

越來越節點值。

/** 
* Getting node value 
* 
* @param Element 
*   node 
* @param key 
*   string 
* */ 
public String getValue(Element item, String str) { 
    NodeList n = item.getElementsByTagName(str); 
    return this.getElementValue(n.item(0)); 
} 

}

調用上面的函數類似這樣的

  Document doc = parser.getDomElement(yourXMLString); 
      NodeList nl = doc.getElementsByTagName("Content"); 

      for (int i = 0; i < nl.getLength(); i++) { 

       Element e = (Element) nl.item(i); 

       // Get your Data Here 

           String base1 =getTagValue("Date",eElement); 
           Date.add(base1); 
           String base2 =getTagValue("Breakfast",eElement); 
           Breakfast.add(base2); 
           String base3 =getTagValue("Lunch",eElement); 
           Lunch.add(base3); 

     } 

改變你這樣的XML

<Contents> 
<content> 
<Date>2/4/2013 
</Date> 
<Breakfast>WG Biscuit, Grits, sausage patty, fruit, juice, milk 
</Breakfast> 
<Lunch>Chicken tenders with sauce, WG affle stick and syrup, carrots-MC, romaine garden salad, fruit, juice, milk 
</Lunch> 
</content> 
<content> 
<Date>2/5/2013 
</Date> 
<Breakfast>grilleed cheese sandich, grits, fruit, juice, milk 
</Breakfast> 
<Lunch>meat sauce w/WG pasta, green beans, caesar salad, WW garlic toast, fruit, juice, milk 
</Lunch> 
</content> 
<content> 
<Date>2/6/2013 
</Date> 
<Breakfast>WG biscuit with chicken patty, fruit, juice, milk 
</Breakfast> 
<Lunch>WG pizza, spinach salad, WKcorn, fruit, juice, milk 
</Lunch> 
</content> 
<content> 
<Date>2/7/2013 
</Date> 
<Breakfast>WG french toast sticks (4), sausage links, fruit, juice, milk 
</Breakfast> 
<Lunch>salisbury steak, black eyed peas, creamed potatoes with gravy, greens-MC, spring mixed salad, WW cornbread, fruit, juice, milk 
</Lunch> 
</content> 
<content> 
<Date>2/8/2013 
</Date> 
<Breakfast>WG breakfast bagel, yogurt, fruit, juice, milk 
</Breakfast> 
<Lunch>BBQ rib portion on WG bun, sweet potato fries or yams, romaine garden salad, fruit, juice, milk 
</Lunch> 
</content> 
<content> 
<Date>2/11/2013 
</Date> 
<Breakfast>Mardi Gras Holiday - No School 
</Breakfast> 
<Lunch /> 
</content> 
</Contents> 

並稱之爲NodeList nl = doc.getElementsByTagName("Content");

+0

我編輯..試試這個,讓我知道這有幫助嗎? –

+0

也使用nl.getLength();方法來獲取您的Nodelist的大小 –

+0

for循環只重複一次。 –