现在的位置: 首页 > 综合 > 正文

android DOM解析Xml

2018年03月31日 ⁄ 综合 ⁄ 共 3109字 ⁄ 字号 评论关闭
People类是自己写的一个类,主要保存各个字符串数据。
  由于没学过Xml语法只能依样画葫芦了呗- -
1.为了具有扩展性 自己编了一个xml文件:
<?xml version="1.0"encoding="utf-8"?>
   <peoples>
       <people
           name="谢XX"age="23" >aaaaaaaaa
          <nationality>中国</nationality>
          <graduation>XXX大学</graduation>
          <introductionname="介绍">谢XX测试描述的句子长长。。。。。。</introduction>
       </people>
       <people
          name="王XX"age="23" >bbbbbbbb
          <nationality>中国</nationality>
          <graduation>XX大学</graduation>
          <introduction>王XX测试描述的句子</introduction>
       </people>
       <people
           name="林XX"age="23" >cccccccc
          <nationality>中国</nationality>
          <graduation>理工大学</graduation>
          <introduction>林XX测试描述的句子</introduction>
       </people>
   </peoples>
   总结:有点像是树形结构。
2.关键解析部分代码:
(1)声明各种需要的类:
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document document = null;
InputStream inputStream = null;
(2)实现DOM解析
list = newArrayList<People>();
factory = DocumentBuilderFactory.newInstance();
try {
//惯例 取得document文件实例的过程
builder = factory.newDocumentBuilder();
inputStream = XmlMainActivity.this.getResources()
.getAssets().open("test.xml");//以工程文件下assets文件夹为根目录
document = builder.parse(inputStream);
//取得根Element 以此列出所有节点NodeList
Element root = document.getDocumentElement();
//getElementsByTagName是在当前的Element 下查找"people"标志并生成NodeList 
NodeList nodes =root.getElementsByTagName_r("people");
//取出每个节点中的数据,这里应该分成3种数据,
//①是name、age那样的Attribute数据;
//②是在<people>括号外的NodeValue数据;
//③最后是在其地下的另一个node数据节点。
for (int i = 0; i < nodes.getLength(); i++){
Element peopleitem = (Element) nodes.item(i);
name = peopleitem.getAttribute("name");
age = peopleitem.getAttribute("age");
//introduction=peopleitem.getFirstChild().getNodeValue();
Element item = (Element) peopleitem
.getElementsByTagName_r("nationality").item(0);
nationality = item.getFirstChild().getNodeValue();
item = (Element) peopleitem.getElementsByTagName_r(
"graduation").item(0);
graduation = item.getFirstChild().getNodeValue();
item = (Element) peopleitem.getElementsByTagName_r(
"introduction").item(0);
introduction =item.getFirstChild().getNodeValue();
// NodeListchildenodes=peopleitem.getElementsByTagName_r("introduction");
// Element childitem=(Element) childenodes.item(0);
// introduction=childitem.getAttribute("name");
// introduction=introduction+"  "+childitem.getFirstChild().getNodeValue();
list.add(new People(name, age, nationality, graduation,
introduction, XmlMainActivity.this));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  根据粗俗的理解:DOM解析过程有点像树遍历过程,
①取得根节点Elementroot =document.getDocumentElement(); 
②取得所有节点列表NodeList nodes =root.getElementsByTagName_r("people");
③取得第i个子节点Element peopleitem = (Element)nodes.item(i);
④取得第i个子节点的Attribute数据、NodeValue数据
⑤列出第i个子节点的"introduction"子节点NodeListchildenodes=peopleitem.getElementsByTagName_r("introduction");
⑥重复③④步骤;
列出第i个子节点的"nationality"子节点NodeList。
重复③④步骤;
...
取得根目录下第i+1个子节点Elementpeopleitem = (Element) nodes.item(i);
再重复

xml虽然复杂了点但是画下图还是很好搞的。
element和document继承Node接口
nodelist本身只是一个接口
document本身代表整个xml文件,element代表某个节点,其中由document.getDocumentElement()取得的element为根节点,
然后从根节点调用root.getElementsByTagName_r("people");可以取得不同标签下的所有Nodelist,
再取得(Element) nodelist.item(i);子节点 
最后可以再再该子节点下调用getElementsByTagName。
整个解析过程可以不按照xml固有的层级关系,即可以直接从根节点下寻找第N层标签,只要逻辑上处理得当。(?木有验证)。

抱歉!评论已关闭.