如何解析嵌套的xml?

时间:2012-03-20 17:32:32

标签: android

我解析了小xml而没有nestes但是我无法解析这个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解析但是在获得学校节点列表之后,我无法获得其他属性,将这些属性设置为coreesponding类,请任何人指导我。我已经搜索谷歌并完成了示例但无法获得任何线索。

3 个答案:

答案 0 :(得分: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");
  }

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自然而且通常允许嵌套标签,事实上,这也是它如此有用的部分原因。

答案 1 :(得分: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*/ 
      }
    }

希望有所帮助

答案 2 :(得分: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];

它将返回数据集。

相关问题