进入正题:
1、待解析的XML:
<?xml version="1.0" encoding="utf-8"?> <mobile> <Response> <list> <item> <id>39</id> <username>user1</username> <content>content1</content> <flag>1</flag> <urlstr>20140103013946_jygy.amr</urlstr> </item> <item> <id>38</id> <username>user2</username> <content>content2</content> <flag>3</flag> <urlstr>20140103013621_jygy.jpg</urlstr> </item> </list> </Response> </mobile>
2、根据上面的XML定义实体
Item.java
public class Item { private String id; private String username; private String content; private String flag; private String urlstr; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getFlag() { return flag; } public void setFlag(String flag) { this.flag = flag; } public String getUrlstr() { return urlstr; } public void setUrlstr(String urlstr) { this.urlstr = urlstr; } @Override public String toString() { return "Item [id=" + id + ", username=" + username + ", content=" + content + ", flag=" + flag + ", urlstr=" + urlstr + "]"; } }
ItemList.java
public class ItemList { public ArrayList<Item> itemList; public ItemList() { itemList = new ArrayList<Item>(); } public ArrayList<Item> getItemList() { return itemList; } public void setItemList(ArrayList<Item> itemList) { this.itemList = itemList; } @Override public String toString() { return "ItemList [itemList=" + itemList + "]"; } }
3、解析器实现
public class GetItemListHandler extends DefaultHandler { private ItemList result; private final String TAG_ITEM = "item"; boolean isItem; private final String TAG_ID = "id"; boolean isId; private final String TAG_USERNAME = "username"; boolean isUsername; private final String TAG_CONTENT = "content"; boolean isContent; private final String TAG_FLAG = "flag"; boolean isFlag; private final String TAG_USRSTR = "urlstr"; boolean isUrlstr; private Item item; private String curValue = ""; private String curTag = ""; public GetItemListHandler() { super(); result = new ItemList(); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); Debug.print(GetItemListHandler.class, "uri=" + uri + " localName=" + localName + " qName=" + qName + " attributes=" + attributes); if (TAG_ITEM.equals(localName)) { isItem = true; item = new Item(); } else if (TAG_ID.equals(localName)) { isId = true; } else if (TAG_USERNAME.equals(localName)) { isUsername = true; } else if (TAG_CONTENT.equals(localName)) { isContent = true; } else if (TAG_FLAG.equals(localName)) { isFlag = true; } else if (TAG_USRSTR.equals(localName)) { isUrlstr = true; } curValue = ""; curTag = localName; } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); handleElementValue(curTag, curValue); Debug.print(GetItemListHandler.class, "uri=" + uri + " localName=" + localName + " qName=" + qName); if (TAG_ITEM.equals(localName)) { isItem = false; if (item != null) { result.getItemList().add(item); Debug.print(GetItemListHandler.class, "item="+item.toString()); item = null; } } else if (TAG_ID.equals(localName)) { isId = false; } else if (TAG_USERNAME.equals(localName)) { isUsername = false; } else if (TAG_CONTENT.equals(localName)) { isContent = false; } else if (TAG_FLAG.equals(localName)) { isFlag = false; } else if (TAG_USRSTR.equals(localName)) { isUrlstr = false; } curTag = ""; } protected void handleElementValue(String tag, String value) { if (!TextUtils.isEmpty(value) && !TextUtils.isEmpty(tag)) { // 必须是在item里面才处理 if (isItem && item != null) { if (isId) { item.setId(value); } else if (isUsername) { item.setUsername(value); } else if (isContent) { item.setContent(value); } else if (isFlag) { item.setFlag(value); } else if (isUrlstr) { item.setUrlstr(value); } } } } @Override public final void characters(char[] ch, int start, int length) throws SAXException { String str = null; try { str = new String(ch, start, length); } catch (Exception e) { } Debug.print(GetItemListHandler.class, "str=" + str); curValue = str; } public ItemList getResult() { return result; } public void setResult(ItemList result) { this.result = result; } }
4、开始解析测试
public class MainActivity extends Activity {
private SAXParser saxParser;
private GetItemListHandler listHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
saxParser = getSAXParser();
listHandler=new GetItemListHandler();
try{
AssetManager am = getAssets();
InputStream is = am.open("demo.xml");
saxParser.parse(is,
listHandler);
for(Item item:listHandler.getResult().getItemList()){
Debug.print(MainActivity.class, "item="+item.toString());
}
}catch(Exception e){
Debug.print(MainActivity.class, "e="+e.toString());
}
}
private SAXParser getSAXParser() {
if (saxParser == null) {
// 创建sax解析器
SAXParserFactory spf = SAXParserFactory.newInstance(); // 初始化sax解析器
try {
saxParser = spf.newSAXParser();
} catch (ParserConfigurationException e) {
// e.printStackTrace();
} catch (SAXException e) {
// e.printStackTrace();
}
}
return saxParser;
}
}
例子比较简单,可以直接看源码,有日志输出,看看日志基本上可以把SAX解析的流程整理清楚,代码写的比较急,比较粗糙,希望能够抛砖引玉
http://www.eoeandroid.com/forum.php?mod=attachment&aid=MTEzMzE3fGFlNDQyY2I4fDEzODg3NjI0NTJ8MTY3NDJ8MzIwMjQ5
声明:本人所有随笔全部原创,eoe论坛账号mei_ping89