标签:
新建xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<contacts>
<contact id="1">
<name>zhangsan</name>
<image src="http://192.168.1.103:8080/mymyweb/images/1.gif"/>
</contact>
<contact id="2">
<name>zhangsan</name>
<image src="http://192.168.1.103:8080/mymyweb/images/1.gif"/>
</contact>
</contacts>
安卓版本dom解析:
private void createXmlFile(){
File linceseFile=new File(BOOKS_PATH);tv.setText(res);
其中注意设定权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
安卓版本pullparser解析:
try {
XmlPullParserFactory factory=XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp=factory.newPullParser();
xpp.setInput(new StringReader("<contacts><contact id=‘1‘> <name>zhangsan</name> <image src=‘http://192.168.1.103:8080/mymyweb/images/1.gif‘/></contact></contacts>"));
int eventType=xpp.getEventType();
while(eventType!=XmlPullParser.END_DOCUMENT){
switch(eventType){
case XmlPullParser.START_DOCUMENT:
System.out.println("Start document");
break;
case XmlPullParser.START_TAG:
System.out.println("Start tag"+xpp.getName());
if(xpp.getName().equals("name")){
Toast.makeText(getApplicationContext(), xpp.nextText(), Toast.LENGTH_SHORT).show();
}else if(xpp.getName().equals("image")){
Toast.makeText(getApplicationContext(), xpp.getAttributeValue(0), Toast.LENGTH_SHORT).show();
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_DOCUMENT:
break;
default :
break;
}
eventType=xpp.next();
}
java中的dom,XmlPullParser 解析xml,
标签:
原文地址:http://blog.csdn.net/ligaogang/article/details/45953191