使用parser.next()可进入下一个元素并触发相应事件,可使用一个switch对感兴趣的事件进行选择和处理。
调用parser.nextText()获取下一个Text类型元素的值。
可在www.xmlpull.org下载Pull解析器源码和文档。
【特点】结构简单(一个借口,一个例外,一个工厂组成);
简单易用(只用一个重要方法next()检索下一个事件,
5个事件:START_DOCUMENT,START_TAG,TEXT,END_TAG,END_DOCUMENT);
多功能性,最小的需求
使用步骤:
①:针对从XML中获取到的信息,需对其建立一个对象(例:Person),存放相关信息
②:由Xml.newPullParser()创建XmlPullParser
③:设置资源并指定解析编码 pullParser.setInput(is,"UTF-8");
④:测试需要,建立一个person.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<persons>
<person id="23">
<name>Test</name>
<age>21</age>
</person>
</persons>
⑤:在应用中通过
InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("person.xml");
加载需要解析的文件
*****************************
⑥:生成(保存)xml文档:
通过Xml.newSerializer()获取XmlSerializer序列生成器
设置编码:xSerializer.setOutput(outputStream,"UTF-8");
xSerializer.startDocument("UTF-8",true);
建立File,以OutputStream形式进行生成
【附】:android应用单元测试方法
①:在AndroidManifest.xml中添加节点
<uses-library android:name="android.test.runner" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.gzu.xmlparsertest" />
②:建立测试类继承AndroidTestCase
【源代码如下:】
*/
/****************************************【源码】START**************************************************/
//1.Person.java
public class Person { private Integer id; private String name; private short age; public Person() { super(); } public Person(String name,short age) { super(); this.name = name; this.age = age; } public Person(Integer id,String name,short age) { super(); this.id = id; this.name = name; this.age = age; } // getter and setter ... }
//2.PullParseService.java
public class PullParseService { /** Pull 解析Xml文档 */ public static List<Person> getPersons(InputStream is) throws Exception{ List<Person> persons = null; Person person = null; XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(is,"UTF-8"); int eventType = pullParser.getEventType(); while(eventType!=XmlPullParser.END_DOCUMENT){ switch(eventType){ case XmlPullParser.START_DOCUMENT: persons = new ArrayList<Person>(); break; case XmlPullParser.START_TAG: if("person".equals(pullParser.getName())){ int id = Integer.valueOf(pullParser.getAttributeValue(0)); person = new Person(); person.setId(id); } if(person != null){ if("name".equals(pullParser.getName())){ person.setName(pullParser.nextText()); } if("age".equals(pullParser.getName())){ person.setAge(new Short(pullParser.nextText())); } } break; case XmlPullParser.END_TAG: if("person".equals(pullParser.getName())){ persons.add(person); person = null; } break; } eventType = pullParser.next(); } return persons; } }
//3.单元测试类 SAXParserTest.java
public class SAXParserTest extends AndroidTestCase { private static final String TAG = "SAXParserTest"; public void testPullGetPerson() throws Throwable{ InputStream is = this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons = PullParseService.getPersons(is); String text = ""; for(Person p:persons){ text = text + "ID: "+p.getId()+" Name: "+p.getName()+" Age: "+p.getAge()+"\n"; } Log.i(TAG,text); } }
//4.MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView main = (TextView) findViewById(R.id.main_content); InputStream is = MainActivity.class.getClassLoader().getResourceAsStream("person.xml"); try { List<Person> persons = testPull(is); String text = "Pull "; for(Person p:persons){ text = text + "ID: "+p.getId()+" Name: "+p.getName()+" Age: "+p.getAge()+"\n"; } main.setText(text); } catch (Exception e) { e.printStackTrace(); } } private List<Person> testPull(InputStream is) throws Exception{ return DomParserService.getPersons(is); } }
//6.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.gzu.xmlparsertest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner" /> <activity android:name="cn.gzu.xmlparsertest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.gzu.xmlparsertest" /> </manifest>
//7.xml生成方法(PullParseService.java中)
/** Pull 保存Xml文档 */
public static void save(List<Person> persons,OutputStream os) throws Exception{ XmlSerializer xSerializer = Xml.newSerializer(); xSerializer.setOutput(os,"UTF-8"); xSerializer.startDocument("UTF-8",true); xSerializer.startTag(null,"persons"); for(Person person : persons){ xSerializer.startTag(null,"person"); xSerializer.attribute(null,"id",person.getId().toString()); xSerializer.startTag(null,"name"); xSerializer.text(person.getName()); xSerializer.endTag(null,"name"); xSerializer.startTag(null,"age"); xSerializer.text(String.valueOf(person.getAge())); xSerializer.endTag(null,"age"); xSerializer.endTag(null,"person"); } xSerializer.endTag(null,"persons"); xSerializer.endDocument(); os.flush(); os.close(); }//8.测试生成类方法 public void testPullSavePerson() throws Throwable{ File file = new File(this.getContext().getFilesDir(),"persons.xml"); FileOutputStream outputStream = new FileOutputStream(file); List<Person> persons = new ArrayList<Person>(); persons.add(new Person(101,"张五常",(short)21)); persons.add(new Person(102,"刘丽",(short)20)); persons.add(new Person(103,"吴雪雪",(short)19)); persons.add(new Person(104,"莉莉",(short)23)); PullParseService.save(persons,outputStream); Log.i(TAG,"***************** testPullSavePerson **************"); } //9.生成结果 persons.xml <?xml version='1.0' encoding='UTF-8' standalone='yes' ?> <persons> <person id="101"> <name>张五常</name> <age>21</age> </person> <person id="102"> <name>刘丽</name> <age>20</age> </person> <person id="103"> <name>吴雪雪</name> <age>19</age> </person> <person id="104"> <name>莉莉</name> <age>23</age> </person> </persons> /****************************************【源码】END**************************************************/ 原文链接:https://www.f2er.com/xml/297935.html