Android dom解析XML数据

    技术2022-07-02  98

     

    /* 除了使用 SAX可以解析XML文件,大家也可以使用熟悉的DOM来解析XML文件。 DOM解析XML文件时,会将XML文件的所有内容以文档树方式存放在内存中,然后允许您使用DOM API遍历XML树、检索所需的数据。使用DOM操作XML的代码看起来是比较直观的,并且在编码方面比基于SAX的实现更加简单。但是,因为DOM需要将XML文件的所有内容以文档树方式存放在内存中,所以内存的消耗比较大,特别对于运行Android的移动设备来说,因为设备的资源比较宝贵,所以建议还是采用SAX来解析XML文件,当然,如果XML文件的内容比较小采用DOM也是可行的。 */

     

    <?xml version="1.0" encoding="UTF-8"?> <persons> <person id="23"> <name>zhangsan</name> <age>25</age> </person> <person id="20"> <name>lisi</name> <age>28</age> </person> </persons>

     

    package com.zyq.service; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.zyq.voo.Person; /** * 采用DOM完成XML的解析 * @author Administrator * */ public class DOMPersonService { /** * DOM 解析person.xml * @param inputStream person.xml数据 * @return persons * @throws Exception */ public List<Person> getPersons(InputStream inputStream) throws Exception { List<Person> persons=new ArrayList<Person>(); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document document=builder.parse(inputStream); Element element=document.getDocumentElement(); NodeList nodeList=element.getElementsByTagName("person"); for(int i=0;i<nodeList.getLength();i++) { Element personNode=(Element)nodeList.item(i); Integer id=new Integer(personNode.getAttribute("id")); Person person=new Person(); person.setId(id); NodeList childNodes=personNode.getChildNodes(); for(int j=0;j<childNodes.getLength();i++) { if(childNodes.item(j).getNodeType()==Node.ELEMENT_NODE) { if("name".equals(childNodes.item(j).getNodeName())) { String name=childNodes.item(j).getFirstChild().getNodeName(); person.setName(name); } else if("age".equals(childNodes.item(j).getNodeName())) { Short age=new Short(childNodes.item(j).getFirstChild().getNodeName()); person.setAge(age); } } } persons.add(person); } return persons; } }

     

    package com.zyq.voo; public class Person { private Integer id; private String name; private Short age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Short getAge() { return age; } public void setAge(Short age) { this.age = age; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + "]"; } }

     

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zyq.xml" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <uses-library android:name="android.test.runner" /> <activity android:name=".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> <uses-sdk android:minSdkVersion="8" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.zyq.xml" android:label="Tests for My App" /> </manifest>

     

    package com.zyq.xml; import java.io.InputStream; import java.util.List; import com.zyq.service.DOMPersonService; import com.zyq.service.SAXPersonService; import com.zyq.voo.Person; import android.test.AndroidTestCase; import android.util.Log; public class PersonServiceTest extends AndroidTestCase { private static final String TAG="PersonServiceTest"; public void testSAX() throws Throwable { SAXPersonService personService=new SAXPersonService(); InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons=personService.getPersons(inputStream); for(Person person : persons) { Log.i(TAG, person.toString()); } } public void testDOM() throws Throwable { DOMPersonService personService=new DOMPersonService(); InputStream inputStream=this.getClass().getClassLoader().getResourceAsStream("person.xml"); List<Person> persons=personService.getPersons(inputStream); for(Person person : persons) { Log.i(TAG, person.toString()); } } }

     


    最新回复(0)