Android DOM 解析 XML

openkk 12年前

Android DOM 解析 XML

res/raw/person.xml

<?xml version="1.0" encoding="UTF-8"?>  <persons>      <person id="001">          <name>张三</name>          <age>22</age>      </person>            <person id="002">          <name>李四</name>          <age>23</age>      </person>            <person id="003">          <name>王五</name>          <age>24</age>      </person>  </persons>

DomParse.java

public class DomParse {   public static List<Person> parse(InputStream  inputStream){    List<Person> persons=new ArrayList<Person>();    try {     //创建解析器     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();     DocumentBuilder builder=factory.newDocumentBuilder();     Document dom = builder.parse(inputStream);     //得到根节点,     Element root = dom.getDocumentElement();     //查找所有persons节点,     NodeList pNodes = root.getElementsByTagName("person");     //**********************遍历所有person节点**********************     for (int i = 0; i < pNodes.getLength(); i++) {      Person person = new Person();      //得到第一个person节点                  Element pNode = (Element) pNodes.item(i);                  //获取person节点的id属性值                  person.setId(new Integer(pNode.getAttribute("id")));                  //获取person节点下的所有子节点                  NodeList cNodes = pNode.getChildNodes();                  //**********************遍历person节点下的所有子节点**********************                  for (int j = 0; j < cNodes.getLength(); j++) {                   Node node = (Node) cNodes.item(j);                   //判断是否为元素类型                   if(node.getNodeType() == Node.ELEMENT_NODE){                    Element cNode = (Element) node;                       //判断是否为name和age元素                          if ("name".equals(cNode.getNodeName())) {                                     person.setName(cNode.getFirstChild().getNodeValue());                          }else if ("age".equals(cNode.getNodeName())) {                                      person.setAge(new Short(cNode.getFirstChild().getNodeValue()));                          }                   }                  }                  persons.add(person);     }     inputStream.close();    } catch (Exception e) {     e.printStackTrace();    }    return persons;   }  }
在主类中调用:

public class TestActivity extends Activity {   Button btn;   ListView listView;   List<String> list=new ArrayList<String>();      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          btn=(Button) findViewById(R.id.button);          listView=(ListView) findViewById(R.id.listView);          btn.setOnClickListener(new OnClickListener(){     public void onClick(View arg0) {      InputStream inputStream=getResources().openRawResource(R.raw.person);      List<Person>persons = DomParse.parse(inputStream);      for (Iterator iterator = persons.iterator(); iterator.hasNext();) {       Person person = (Person) iterator.next();       list.add(String.valueOf("ID:"+person.getId()) + " 姓名: "         + person.getName() + " 年龄: " + person.getAge());      }      ArrayAdapter<String> adapter = new ArrayAdapter<String>(        getApplicationContext(),        android.R.layout.simple_list_item_1,        list);      listView.setAdapter(adapter);     }          });      }  }