1:androidmanifest.xml的内容:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.itcast.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="cn.itcast.xml" android:label="Tests for My App" /> </manifest>
其中对其测试环境进行了搭建
2:要读取的xml文件的内容为:test.xml
<?xml version="1.0" encoding="UTF-8"?> <persons> <person id="28"> <name>bo</name> <age>28</age> </person> <person id="29"> <name>xiaohong</name> <age>21</age> </person> </persons>
3:使用DOM技术读取xml文件的程序XMLService
public class XMLService { public List<Person> getPerson(InputStream inStream) throws Throwable { List<Person> persons = new ArrayList<Person>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(inStream); /* * <persons> <person id="28"> <name>zhangbo</name> <age>28</age> </person> <person id="29"> <name>xiaohong</name> <age>21</age> </person> </persons> */ Element element = document.getDocumentElement(); //获得根结点 NodeList personNodes = element.getElementsByTagName("person"); //根据person获得结点 for(int i = 0; i<personNodes.getLength(); i++) { Person person = new Person(); Element personElement = (Element) personNodes.item(i); person.setId(new Integer(personElement.getAttribute("id"))); NodeList childNodes = personElement.getChildNodes(); for(int j = 0; j<childNodes.getLength(); j++) { if(childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) { //判断当前结点是否是元素结点 Element childElement = (Element)childNodes.item(j); if("name".equals(childElement.getNodeName())) { person.setName(childElement.getFirstChild().getNodeValue()); } else if("age".equals(childElement.getNodeName())) { person.setAge(new Short(childElement.getFirstChild().getNodeValue())); } } } persons.add(person); } return persons; } }
4:使用到的Person类
public class Person { private Integer id; private String name; private Short age; public Person(){} public Person(Integer id, String name, Short age) { super(); this.id = id; this.name = name; this.age = 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 [age=" + age + ", id=" + id + ", name=" + name + "]"; } }
4:测试类XMLTest
public class XMLTest extends AndroidTestCase { private static final String TAG = "XMLTest"; public void testDomGetPersons() { XMLService xmlService = new XMLService(); InputStream inputStream = getClass().getClassLoader().getResourceAsStream("test.xml"); try { List<Person> persons = xmlService.getPerson(inputStream); for(Person person : persons) { Log.i(TAG, person.toString()); } } catch (Throwable e) { e.printStackTrace(); } } }
5:测试结果
在控制台输出Person[age=28, id=28, name=bo]
Person[age=21, id=29, name=xiaohong]