Sax解析XML高效

前端之家收集整理的这篇文章主要介绍了Sax解析XML高效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<?xml version="1.0" encoding="UTF-8"?>

<Service id="recomment" >
<Menus>
<menu>
<id><![CDATA[789942]]></id>
<name><![CDATA[悬崖上的金鱼公主]]></name>
<image><![>url</image>
<action><![CDATA[GetMoiveDetail]]></action>
<actionURL><![CDATA[url]]></actionURL>
<type><![CDATA[少儿]]></type>

</menu>

</Menus>

</Service>

//获取sd卡路径
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();//获取跟目录
}
return sdDir.toString();
}

// 解析xml,本地可以和网络不可以
private void saxParseXml(String url) {
try {
FileInputStream fileIS = new FileInputStream(url);
StringBuffer sb=new StringBuffer();
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
//just reading each line and pass it on the debugger
while((readString = buf.readLine())!= null){
sb.append(readString);
}
//创建一个SAX的解析器
SAXParserFactory parsefac=SAXParserFactory.newInstance();
XMLReader reader=parsefac.newSAXParser().getXMLReader();
//为XMLreader创建解析容器
reader.setContentHandler(new MyContentHandler());
//开始解析文件
String s1=sb.toString();
reader.parse(new InputSource(new StringReader(s1)));
} catch (Exception e) {
e.printStackTrace();
}
}
String content = null;
//在这就要复写Handler类方法了.在各个事件中写自己想要实现的效果
class MyContentHandler implements org.xml.sax.ContentHandler {
@Override
public void characters(char[] ch,int start,int length)throws SAXException {
content = new String(ch,start,length);
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endElement(String uri,String localName,String qName)throws SAXException {
if(localName.equals("menu")) {
Message msg = new Message();
msg.arg1 = ISTouchSystemMessageID.TYPE.INSIDE;
msg.arg2 = InsideMessageID.ADD_DATA_RECOMMEND;
msg.obj = mPosterInfo;
mHandler.sendMessage(msg);
mPosterInfo = null;
}
else if(localName.equals("id")) {
mPosterInfo.setmId(content);
}
else if(localName.equals("action")) {
mPosterInfo.setmAction(content);
}
else if(localName.equals("name")) {
mPosterInfo.setmMovieName(content);
}
else if (localName.equals("actionURL")) {
mPosterInfo.setmActionUrl(content);
}
else if(localName.equals("image")) {
mPosterInfo.setmThumPath(content);
}
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void ignorableWhitespace(char[] ch,int length)throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void processingInstruction(String target,String data)throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
@Override
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startElement(String uri,String qName,Attributes atts) throws SAXException {
if (localName.equals("menu")) {
mPosterInfo = new RecommendData();
}
}
@Override
public void startPrefixMapping(String prefix,String uri)throws SAXException {
// TODO Auto-generated method stub
}
}

//在收消息的地方

mPosterInfo = (RecommendData)msg.obj;
if(mRecommendInfoList != null && mRecommendInfoList.size() < 7){
mRecommendInfoList.add(mPosterInfo);
}

用的时候;info.mName = mRecommendInfoList.get(arg2).mMovieName;

private ArrayList<RecommendData> mRecommendInfoList = new ArrayList<RecommendData>();

可参考: http://www.cnblogs.com/felix-hua/archive/2012/01/10/2317404.html

http://gundumw100.iteye.com/blog/845436

http://www.apkbus.com/android-78091-1-1.html

http://sinye.iteye.com/blog/763895

http://www.jb51.cc/article/p-akrgskrn-bgv.html

原文链接:https://www.f2er.com/xml/299318.html

猜你在找的XML相关文章