2012-03-20 125 views
0

我解析XML小,並沒有nestes但我unnable解析這個XML的 如何解析嵌套的xml?

<screen id = "1" backgroundColor = "" backgroundImg = "" templateId = "12" hasNavigationBar = "0" hasTabBar = "1" > 

    <navigation-bar title = "" color = "#b0b0b0" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 


    <tab-bar numberOfTabs = "4" > 
     <tab-bar-item title = "Home" image = "tab_home.png" linkedScreen = "101" /> 
     <tab-bar-item title = "About Us" image = "tab_about.png" linkedScreen = "201" /> 
     <tab-bar-item title = "Request Info" image = "tab_requestinfo.png" linkedScreen = "301" /> 
     <tab-bar-item title = "Directions" image = "tab_directions.png" linkedScreen = "401" /> 
     <!--<tab-bar-item title = "Current Parents" image = "tab_family.png" linkedScreen = "501" />--> 
    </tab-bar> 

</screen> 

<screen id = "101" backgroundColor = "" backgroundImg = "HomeBg.png" templateId = "11" hasNavigationBar = "0" hasTabBar = "1" > 

    <navigation-bar title = "" color = "#b0b0b0" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 
    <button-view yOffset = "94" spacing = "5" /> 
    <button width = "273" height = "33" image = "Btn_About.png" action = "201" textColor = "#ffffff">About Brearley</button> 
    <button width = "273" height = "33" image = "Btn_Admissions.png" action = "103" textColor = "#ffffff">Admission</button> 
    <button width = "273" height = "33" image = "Btn_Calendar.png" action = "102" textColor = "#ffffff">School Calendar</button> 
    <button width = "273" height = "33" image = "Btn_Giving.png" action = "901" textColor = "#ffffff">Giving</button> 
    <button width = "273" height = "33" image = "Btn_Directions.png" action = "401" textColor = "#ffffff">Directions</button> 
    <button width = "273" height = "33" image = "Btn_Families.png" action = "701" textColor = "#ffffff">Families</button> 
    <button width = "273" height = "33" image = "Btn_Alumnae.png" action = "801" textColor = "#ffffff">Alumnae</button> 

</screen> 

<screen id = "102" backgroundColor = "#dedede" backgroundImg = "" templateId = "16" hasNavigationBar = "1" hasTabBar = "1" > 

    <navigation-bar title = "School Calendar" color = "#b0b0b0" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 


    <calender-view > 
     <calender-url>http://www.crescerance.com/School/brearley/calendar/AcademicEvent.html</calender-url> 
     <event date="September 5, 2011" day = "Monday" time = "8:00 AM" title = "Labor Day Holiday" desc = "Demo School will be closed today for the Labor Day Holiday." /> 
     <event date="September 6, 2011" day = "Tuesday" time = "8:30 AM" title = "First Day of School" desc = "First day of school for all students. Don't forget to pack a pair of extra clothes, indoor shoes, water bottle, sunscreen, and bugspray." /> 
     <event date="October 17, 2011" day = "Monday" time = "8:00 AM - 6:00PM" title = "Teacher Work Day" desc = "School is closed for a Teacher Work Day. Enjoy the long weekend!" /> 
     <event date="November 21, 2011" day = "Monday" time = "8:00 AM" title = "Parent Conferences Day 1" desc = "Please check the Parent Conferences schedule at school and ensure that both parents can attend." /> 
     <event date="November 22, 2011" day = "Tuesday" time = "12:00 AM" title = "Parent Conferences Day 2" desc = "Please check the Parent Conferences schedule at school and ensure that both parents can attend." /> 
     <event date="January 2, 2012" day = "Monday" time = "8:00 AM" title = "School Resumes" desc = "School resumes after the Winter Break." /> 
     <event date="January 16, 2012" day = "Monday" time = "8:00 AM" title = "MLK Holiday" desc = "School is closed for the Martin Luther King Holiday." /> 
     <event date="February 17, 2012" day = "Friday" time = "8:00 AM" title = "Teacher Enrichment Day" desc = "School is closed for Teacher Enrichment." /> 
     <event date="February 20, 2012" day = "Monday" time = "8:00 AM" title = "President's Day Holiday" desc = "School is closed for the President's Day Holiday." /> 
     <event date="May 28, 2012" day = "Monday" time = "8:00 AM" title = "Memorial Day Holiday" desc = "School is closed for the Memorial Day Holiday." /> 
    </calender-view> 

</screen> 

<screen id = "103" backgroundColor = "#dedede" backgroundImg = "InnerPages_BG.png" templateId = "4" hasNavigationBar = "1" hasTabBar = "1" > 

    <navigation-bar title = "Admission" color = "#b0b0b0" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 

    <table-view noOfSection = "1"> <!-- TableView --> 
     <table-section title = "" noOfRows = "4"> 
      <table-row isClickable = "1" action= "1031" hasAccessory = "1" backgroundColor = "#d53044" textColor = "#ffffff" >Welcome</table-row> 
     </table-section> 
     <table-section noOfRows = "2"> 
      <table-row isClickable = "1" action= "1032" hasAccessory = "1" backgroundColor = "#d53044" textColor = "#ffffff" >Applying</table-row> 
      <table-row isClickable = "1" action= "1033" hasAccessory = "1" backgroundColor = "#d53044" textColor = "#ffffff" >Admission Calender</table-row> 
      <table-row isClickable = "1" action= "1034" hasAccessory = "1" backgroundColor = "#d53044" textColor = "#ffffff" >Tuition and Financial Aid</table-row> 
     </table-section> 
     <table-section noOfRows = "2"> 
      <table-row isClickable = "1" action= "1035" hasAccessory = "1" backgroundColor = "#d53044" textColor = "#ffffff" >F.A.Q</table-row> 
     </table-section> 
    </table-view> 

</screen> 

<screen id = "1031" backgroundColor = "#dedede" backgroundImg = "InnerPages_BG.png" templateId = "6" hasNavigationBar = "1" hasTabBar = "1" > 

    <navigation-bar title = "Welcome" color = "#b0b0b0" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 

    <table-view noOfSection = "1"> <!-- TableView --> 
     <table-section title = "" noOfRows = "1"> 
     <table-row isClickable = "0" action= "" height = "345" >http://www.crescerance.com/School/brearley/admission/Welcome.html</table-row> 
     </table-section> 
    </table-view> 

</screen> 

我使用DOM paarsing但得到學校的節點列表後,我unnable讓其他atrribute一組這樣的屬性到coreesponding類請任何人指導我。我allready搜索谷歌和完成的例子,但不能得到任何線索。

回答

0

看看XmlPullParser

從文檔

public class SimpleXmlPullApp {

public static void main (String args[]) 
    throws XmlPullParserException, IOException 
{ 

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    XmlPullParser xpp = factory.newPullParser(); 
    xpp.setInput(new StringReader ("<foo>Hello World!</foo>")); 
    int eventType = xpp.getEventType(); 
    while (eventType != XmlPullParser.END_DOCUMENT) { 
     if(eventType == XmlPullParser.START_DOCUMENT) { 
      System.out.println("Start document"); 
     } else if(eventType == XmlPullParser.START_TAG) { 
      System.out.println("Start tag "+xpp.getName()); 
     } else if(eventType == XmlPullParser.END_TAG) { 
      System.out.println("End tag "+xpp.getName()); 
     } else if(eventType == XmlPullParser.TEXT) { 
      System.out.println("Text "+xpp.getText()); 
     } 
     eventType = xpp.next(); 
    } 
    System.out.println("End document"); 
    } 

}

XML自然和正常允許嵌套的標籤,其實,這就是爲什麼它是如此有用的部分。

0

你是什麼意思的學校節點列表?你能指定你有問題的標籤嗎?

你可以這樣獲得的屬性值:

t.getAttribute("<attributename>"); 

其中t爲類型元素的對象。

假設你有屏幕元素,那麼你可以這樣做

NodeList Nl = Screen.getElementsByTagName("tab-bar-item"); 
    if(Nl!=null && Nl.size()>0){ 
     for(int i=0;i<Nl.size();i++){ 
     Element tab_bar_item = (Element) NL.item(i); 
     String title = tab_bar_item.getAttribute("title"); 
     /*you can get other attributes in the same way*/ 
     } 
    } 

希望幫助

+0

allready得到但如何得到tab_bar,atb_bar_items,按鈕等... – LOOSER 2012-03-20 17:54:41

0

嗨隊友使用此代碼獲取標籤欄:

XmlDocument doc1= new XmlDocument(); 
DataSet dsAPDP = new DataSet(); 
DataTable dtAPDP = new DataTable(); 
    using (XmlReader reader = new XmlNodeReader(doc1.DocumentElement)) 
    { 
     dsAPDP.ReadXml(reader); reader.Close(); 
    } 
dtAPDP = dsAPDP.Tables[0]; 

它將返回數據集。