2013-03-04 181 views
0
<?xml version="1.0" encoding="UTF-8"?> 
<feed> 
    <entry> 
     <id>1</id> 
     <name>Margherita</name> 
     <cost>155</cost> 
     <description>Single cheese topping</description> 
     <list> 
      <author>PPShein</author> 
      <authorurl>http://www.ppshein.net</authorurl> 
     </list> 
    </entry> 
    <entry> 
     <id>2</id> 
     <name>Double Cheese Margherita</name> 
     <cost>225</cost> 
     <description>Loaded with Extra Cheese</description> 
     <list> 
     <author>Mike</author> 
     <authorurl>http://www.mike.net</authorurl> 
    </list>   
    </entry> 
</feed> 

我有問題解析上面的XML文件。通過使用android的this example解析「id,名稱,成本和描述」是很好的。Android解析子XML標記

<author><authorurl>標籤,例如上面的鏈接無法完成我的任務。請幫我解析這個XML文件的子標籤。

回答

0

只需經過一個DOM解析器。您可以通過名稱獲取特定的節點。其實它的工作同其他節點,你只得到了NodeListlist元素具有子節點authorauthorurl

看看這些

Tutorial 1 Tutorial 2

0

1.I已經解析這個XML我想這一個幫助你

<?xml version="1.0" encoding="utf-8"?> 
<rootelement1> 
    <subelement> Hello XML Sub-Element 1</subelement> 
    <subelement> Hello XML Sub-Element 2 
     <subsubelement> 
      Sub Sub Element 
     </subsubelement> 
    </subelement> 
</rootelement1> 

2.extratct XML是在這裏

private String getEventsFromAnXML(Activity activity) 
      throws XmlPullParserException, IOException { 
     StringBuffer stringBuffer = new StringBuffer(); 
     Resources res = activity.getResources(); 
     XmlResourceParser xpp = res.getXml(R.xml.parsxml); 
     xpp.next(); 
     int eventType = xpp.getEventType(); 
     while (eventType != XmlPullParser.END_DOCUMENT) { 
      if (eventType == XmlPullParser.START_DOCUMENT) { 
       stringBuffer.append("--- Start XML ---"); 

      } else if (eventType == XmlPullParser.START_TAG) { 
       stringBuffer.append("\nSTART_TAG: " + xpp.getName()); 
       System.out.println("\n--- Start XML ---" + xpp.getName()); 
      } else if (eventType == XmlPullParser.END_TAG) { 
       stringBuffer.append("\nEND_TAG: " + xpp.getName()); 
      } else if (eventType == XmlPullParser.TEXT) { 
       stringBuffer.append("\nTEXT: " + xpp.getText()); 
      } 
      eventType = xpp.next(); 
     } 
     stringBuffer.append("\n--- End XML ---"); 
     System.out.println("\n--- End XML ---"); 
     return stringBuffer.toString(); 
    } 

Reference from here