我解析使用SAX解析器大的XML文檔,但看到的對谷歌SAX解析的例子之後,我無法解析這個XML:解析XML包含多個屬性
<?xml version="1.0" encoding="UTF-8"?>
<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>
這不是完整的XML文檔。用於解析的是,我提出5屬性類:
1)School
public class School
{
public String title;
public String icon="";
public String browserBackButtonTitle;
public String navBarColor;
public ArrayList<Screen> screenlist = new ArrayList<Screen>();
}
2)Screen
public class Screen
{
public String Id;
public String backgroundColor;
public String backgroundImg ;
public String templateId;
public String hasNavigationBar;
public String hasTabBar;
public ArrayList<NavigationBar> objlistofNB = new ArrayList<NavigationBar>();
}
3)NavigationBar
public class NavigationBar
{
public String title;
public String color;
public String backButtonTitle;
}
4)ScreenTabBar
public class ScreenTabBar
{
private int numberOfTabs;
private ArrayList<TabBarItem> objlistofTabBarItem = new ArrayList<TabBarItem>();
}
5)TabBarItem
public class TabBarItem
{
public String Title;
public String image;
public String linkedScreen;
}
我推翻了startElement
方法中,我不能這樣做如何運用精確codition-
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
super.startElement(uri, localName, qName, attributes);
currentElement = true;
if (localName.equals("school"))
{
/** Start */
objschool = new School();
schoolmap = new HashMap<String, String>();
schoolmap.put("school_name", attributes.getValue("title"));
schoolmap.put("school_icon", attributes.getValue("icon"));
schoolmap.put("school_browserBackButtonTitle", attributes.getValue("browserBackButtonTitle"));
schoolmap.put("school_navBarColor", attributes.getValue("navBarColor"));
objschool = (School)parseProperty(objschool,schoolmap);
}
else if (localName.equals("screen"))
{
/** Get attribute value */
objscreen = new Screen();
screenmap.put("screen_id", attributes.getValue("id"));
screenmap.put("screen_backgroundColor", attributes.getValue("backgroundColor"));
screenmap.put("screen_backgroundImg",attributes.getValue("backgroundImg"));
screenmap.put("screen_templateId",attributes.getValue("templateId"));
screenmap.put("screen_hasNavigationBar",attributes.getValue("hasNavigationBar"));
screenmap.put("screen_hasTabBar",attributes.getValue("hasTabBar"));
objscreen =(Screen)parseProperty(objscreen,screenmap);
}
else if (localName.equals("navigation-bar"))
{
objnavBar = new NavigationBar();
navimap.put("naviTitle",attributes.getValue("title"));
navimap.put("navicolor",attributes.getValue("color"));
navimap.put("navibackButtonTitle",attributes.getValue("backButtonTitle"));
objnavBar =(NavigationBar)parseProperty(objnavBar,navimap);
}
else if (localName.equals("tab-bar"))
{
objtabBar = new ScreenTabBar();
tabbarmap.put("numberOfTabs",attributes.getValue("numberOfTabs"));
objtabBar = (ScreenTabBar)parseProperty(objtabBar, tabbarmap);
}
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"));
}
}
的parseProperty
方法是:
private Object parseProperty(Object t, Map<String,String> list)
{
Class c=null;
String className[]=t.toString().split("@");
try {
String s = t.toString();
c = Class.forName(className[0]);
Field[] f = c.getDeclaredFields();
//for(int i=0; i<list.entrySet().size();i++)
//{
for (String key : list.keySet()) {
System.out.println("key/value: " + key + "/"+list.get(key));
String attrname = key;
for(int j=0; j<f.length;j++)
{
String s2=f[j].getName();
if(s2.equals(attrname))
{
f[j].set(t,list.get(key));
}
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return t;
}
我怎樣才能解析上述的XML以上規則是由我應用的?
你似乎使用SAX解析器。看看DOM解析器,我敢打賭,你會發現更容易理解。你可以谷歌「android dom解析器」並獲取資源。 – slezica 2012-03-20 11:21:16
但我可以使用xml大小很大嗎? – learner 2012-03-20 11:23:37
「很大」有多大?如果「非常」確實是DOM非常大的話,我仍然會看看其他解析器。盲目編寫SAX(特別是Java)是一件偷偷摸摸,耗時的工作。如果你需要更小的內存空間,Android有一個與SAX非常相似的PULL解析器,但它有一個(在我看來)更加健壯的界面。 – slezica 2012-03-20 11:27:24