需要用Pull解析一个XML文件其中的url指向有些有多指向,而一般的解析过程都不能很好的将多指向的部分处理好,于是自己想了个办法,DEMO如下:
<items>
<item>
<type>video</type>
<name>gee</name>
<url>/mnt/sdcard/dmx.mp4</url>
<x>0</x>
<y>0</y>
<width>800</width>
<height>400</height>
</item>
<item>
<type>image</type>
<name>gee</name>
<url>/mnt/sdcard/qq.jpg</url>
<url>/mnt/sdcard/jj.jpg</url>
<url>/mnt/sdcard/ff.jpg</url>
<x>0</x>
<y>400</y>
<width>400</width>
<height>400</height>
</item>
<item>
<type>image</type>
<name>gee</name>
<url>/mnt/sdcard/qq.jpg</url>
<x>400</x>
<y>400</y>
<width>400</width>
<height>400</height>
</item>
<item>
<type>web</type>
<name>gee</name>
<url>http://wap.baidu.com</url>
<x>0</x>
<y>800</y>
<width>800</width>
<height>480</height>
</item>
</items>
解析用的XMLPULL类如下:
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.util.Log;
public class XmlPull2 {
/*
* 解析XML
*
*/
public static List<HashMap<String,Object>> parserXml(File file,
String encoding,String startNode,String... nodes) {
List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = null;
List<String> urls=null;
try {
XmlPullParser parser = XmlPullParserFactory.newInstance()
.newPullParser();
parser.setInput(new FileReader(file));
int evenType = parser.getEventType();
while (evenType != XmlPullParser.END_DOCUMENT) {
String nodeName = parser.getName();
switch (evenType) {
case XmlPullParser.START_TAG:
if (startNode.equals(nodeName)) {
map = new HashMap<String,Object>();
urls=new ArrayList<String>();
}
for (int i = 0; i < nodes.length; i++) {
if (nodes[i].equalsIgnoreCase(nodeName)) {
String temp = parser.nextText();
if (nodes[i].equals("url")) {
urls.add(temp);
Log.i("TAG","节点:" + nodes[i]
+ "\t----------->\t" + temp);
} else {
map.put(nodes[i],temp);
Log.i("TAG","节点:" + nodes[i]
+ "\t----------->\t" + temp);
}
}
if(urls!=null)
map.put("urls",urls);
}
break;
case XmlPullParser.END_TAG:
if (startNode.equals(nodeName) && map != null) {
list.add(map);
}
break;
}
evenType = parser.next();
}
} catch (XmlPullParserException e) {
Log.e("TAG","解析出错:" + e.getMessage());
} catch (IOException e) {
Log.e("TAG","IO异常:" + e.getMessage());
}
return list;
}
}
测试Demo如下:
import java.io.File;
import java.util.HashMap;
import java.util.List;
import com.example.dao.XmlPull;
import com.example.dao.XmlPull2;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//xml文件的地址
File file = new File("/mnt/sdcard/main.xml");
//子节点名称
String[] nodes = new String[] { "type","name","url","x","y",
"width","height" };
//取到的集合
List<HashMap<String,Object>> list = XmlPull2.parserXml(file,"utf-8",
"item",nodes);
//type类型为Image类型各项值
for (HashMap<String,Object> map : list) {
if (map.get("type").equals("image")) {
System.out.println(map.get("type"));
System.out.println(map.get("name"));
System.out.println(map.get("x"));
System.out.println(map.get("y"));
System.out.println(map.get("width"));
System.out.println(map.get("height"));
List<String> ss = (List<String>) map.get("urls");
for (String s : ss) {
System.out.println(s);
}
}
}
}
}
写得很差..只是觉得网上此类的DEMO真的很少...
原文链接:https://www.f2er.com/xml/298201.html