2012-03-21 132 views
-2

我解析XML的這個只能在xml解析中獲取一個節點元素值?

<school title="The Clifton School" icon="" browserBackButtonTitle = "Clifton App" navBarColor = "#7eb432"> 

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

    <navigation-bar title = "" color = "#7eb432" 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 = "Calendar" image = "tab_calendar.png" linkedScreen = "102" /> 
     <tab-bar-item title = "Menu" image = "tab_menu.png" linkedScreen = "604" /> 
     <tab-bar-item title = "Directions" image = "tab_directions.png" linkedScreen = "401" /> 
     <tab-bar-item title = "Contact" image = "tab_contact.png" linkedScreen = "206" /> 
    </tab-bar> 

</screen> 

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

    <navigation-bar title = "" color = "#7eb432" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 
    <button-view yOffset = "100" spacing = "6" /> 
    <button width = "274" height = "30" image = "blue_home_button.png" action = "201" textColor = "#ffffff">About The Clifton School</button> 
    <button width = "274" height = "30" image = "blue_home_button.png" action = "102" textColor = "#ffffff">School Calendar</button> 
    <button width = "274" height = "30" image = "blue_home_button.png" action = "103" textColor = "#ffffff">Admissions</button> 
    <button width = "274" height = "30" image = "red_home_button.png" action = "601" textColor = "#ffffff">Parent Corner</button> 
    <button width = "274" height = "30" image = "green_home_button.png.png" action = "301" textColor = "#ffffff">Request Information</button> 
    <button width = "274" height = "30" image = "green_home_button.png.png" action = "401" textColor = "#ffffff">Directions</button> 
    <button width = "274" height = "30" image = "green_home_button.png.png" action = "112" textColor = "#ffffff">Tell a Friend about Clifton</button> 

</screen> 

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

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

    <table-view noOfSection = "1"> <!-- TableView --> 
     <table-section title = "" noOfRows = "3"> 
      <table-row isClickable = "1" action= "113" hasAccessory = "1" backgroundColor = "#d5eebb" textColor = "#000000">Clifton Campus</table-row> 
      <table-row isClickable = "1" action= "114" hasAccessory = "1" backgroundColor = "#d5eebb" textColor = "#000000">Clairmont Campus</table-row> 
     </table-section> 
    </table-view> 

</screen> 

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

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

    <table-view noOfSection = "1"> <!-- TableView --> 
     <table-section title = "" noOfRows = "1"> 
      <table-row isClickable = "1" action= "107" hasAccessory = "1" backgroundColor = "#d5eebb" textColor = "#000000">Enrollment Process</table-row> 
     </table-section> 
     <table-section title = "" noOfRows = "3"> 
      <table-row isClickable = "1" action= "104" hasAccessory = "1" backgroundColor = "#d5eebb" textColor = "#000000">Tour</table-row> 
      <table-row isClickable = "1" action= "105" hasAccessory = "1" backgroundColor = "#d5eebb" textColor = "#000000">Tuition</table-row> 
      <table-row isClickable = "1" action= "106" hasAccessory = "1" backgroundColor = "#d5eebb" textColor = "#000000">Enrollment Application</table-row> 
     </table-section> 
    </table-view> 

</screen> 

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

    <navigation-bar title = "Tour" color = "#7eb432" 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/clifton/Tours.html</table-row> 
     </table-section> 
    </table-view> 

</screen> 

<screen id = "105" backgroundColor = "#dedede" backgroundImg = "InnerPages_BG.png" templateId = "6" hasNavigationBar = "1" hasTabBar = "1" > 
    <navigation-bar title = "Tuition" color = "#7eb432" 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/clifton/Tuition.html</table-row> 
     </table-section> 
    </table-view> 
</screen> 

<screen id = "106" backgroundColor = "#dedede" backgroundImg = "" templateId = "7" hasNavigationBar = "1" hasTabBar = "1" > 
    <navigation-bar title = "Enrollment Application" color = "#7eb432" backButtonTitle = "Back"> 
     <!--<navigation-item type = "1" action = "" />--> 
    </navigation-bar> 

    <browser-view> <!-- BrowserView --> 
     <url>http://www.thecliftonschool.org/docs/Enrollment%20APPLICATION.pdf</url> 
    </browser-view> 
</screen> 

但它不工作,首先讓我在這搭性能5個POJO類是XML節點描述上面。 使用這個類進行解析,我卻取值 -

boolean isSchool = false; 
boolean isSCreen = false; 
boolean isNavBar = false; 
boolean istabbar = false; 
Context theContext; 

Map<String,String> schoolmap,screenmap,navimap,tabbarmap,tabbaritem; 

public static School objschool   = null; 
public static Screen objscreen   = null; 
public static NavigationBar objnavBar = null; 
public static ScreenTabBar objtabBar = null; 
public static TabBarItem objtabBaritem = null; 

@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException 
     { 
     super.startElement(uri, localName, qName, attributes); 
     //ArrayList<Screen> screens = new ArrayList<Screen>(); 

     if (localName.equals("school")) 
     { 
      isSchool=true; 
      objschool = new School(); 
      /** Start */  
      String title = attributes.getValue("title"); 
      String icon = attributes.getValue("icon"); 
      String browserBackButtonTitle = attributes.getValue("browserBackButtonTitle"); 
      String school_navBarColor = attributes.getValue("navBarColor"); 

      objschool.setTitle(title); 
      objschool.setIcon(icon); 
      objschool.setBrowserBackButtonTitle(browserBackButtonTitle); 
      objschool.setNavBarColor(school_navBarColor); 

     } 
     else if (localName.equals("screen")) 
     { 
      isSCreen = true; 
     /** Get attribute value */ 
      objscreen = new Screen(); 
      String screen_id = attributes.getValue("id"); 
      String screen_backgroundColor = attributes.getValue("backgroundColor"); 
      String screen_backgroundImg = attributes.getValue("backgroundImg"); 
      String screen_templateId = attributes.getValue("templateId"); 
      String screen_hasNavigationBar = attributes.getValue("hasNavigationBar"); 
      String screen_hasTabBar = attributes.getValue("hasTabBar"); 

      objscreen.setId(screen_id); 
      objscreen.setBackgroundColor(screen_backgroundColor); 
      objscreen.setBackgroundImg(screen_backgroundImg); 
      objscreen.setTemplateId(screen_templateId); 
      objscreen.setHasNavigationBar(screen_hasNavigationBar); 
      objscreen.setHasTabBar(screen_hasTabBar); 
      //screens.add(objscreen); 
     } 
     else if (localName.equals("navigation-bar")) 
     { 
      objnavBar = new NavigationBar(); 
      String navititle = attributes.getValue("title"); 
      String navicolor = attributes.getValue("color"); 
      String navibackButtonTitle = attributes.getValue("backButtonTitle"); 
      objnavBar.setTitlelist(navititle); 
      objnavBar.setColorlist(navicolor); 
      objnavBar.setBackButtonTitlelist(navibackButtonTitle); 
     } 
     else if (localName.equals("tab-bar")) 
     { 
      objtabBar = new ScreenTabBar(); 
      tabbarmap.put("numberOfTabs",attributes.getValue("numberOfTabs")); 

     } 
     else if (localName.equals("tab-bar-item")) 
     { 
      objtabBaritem = new TabBarItem(); 
      tabbarmap.put("Title",attributes.getValue("title")); 
      tabbarmap.put("Image",attributes.getValue("image")); 
      tabbarmap.put("LinkedScreen",attributes.getValue ("linkedScreen")); 
     } 
      //   objschool.screenlist = screens; 
} 

@Override 
public void endElement(String uri, String localName, String qName)throws S AXException 
    { 
    super.endElement(uri, localName, qName); 

    /** set value */ 
    if (localName.equalsIgnoreCase("school") && isSchool && isSCreen) 
    { 
     objschool.setScreenlist(objscreen); 
     isSchool=false; 
     isSCreen = false; 
    } 
     else if (localName.equalsIgnoreCase("screen")&& isSCreen && isNavBar) 
     { 
      objscreen.setObjlistofNB(objnavBar); 
      isSCreen = false; 
      isNavBar = false; 
     } 
     else if(localName.equalsIgnoreCase("navigation-bar")) 
     { 
      isNavBar=false; 
     } 
} 
@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 
    // TODO Auto-generated method stub 
    super.characters(ch, start, length); 

} 

回答

1

你在元素的開始創建新的對象,每次只有一個節點。如果元素名稱相同,它將創建一個新的對象來存儲該元素下的屬性數據,從而丟失前一個對象。解決方案應該是你應該創建Object的ArrayList來存儲和添加Object。對於如:

public ArrayList<objscreen> screens = null; 

和內部起始元素:

if (localName.equals("screen")) 
    { 
     isSCreen = true; 
    /** Get attribute value */ 
     objscreen = new Screen(); 
     objscreen.setId(attributes.getValue("id")); 
     objscreen.setBackgroundColor(attributes.getValue("backgroundColor")); 
     objscreen.setBackgroundImg(attributes.getValue("backgroundImg")); 
     objscreen.setTemplateId(attributes.getValue("templateId")); 
     objscreen.setHasNavigationBar(attributes.getValue("hasNavigationBar")); 
     objscreen.setHasTabBar(attributes.getValue("hasTabBar")); 

     if(screens ==null) 
     screens =new ArrayList<objscreen>(); 
     screens.add(objscreen); 
    }