Android Xml解析之DOM詳細攻略

來源:互聯網
上載者:User

標籤:des   android   style   http   tar   color   

參考地址:http://wenku.baidu.com/view/aca83d12cc7931b765ce15d1.html

由於dom方式是儲存於記憶體中,比較耗記憶體,不過對於一些資料量比較小但又比較常用的隱秘資訊,可以用此方法

People類是自己寫的一個類,主要儲存各個字串資料。1.為了具有擴充性 自己編了一個xml檔案:<?xml version="1.0" encoding="utf-8"?>    <peoples>        <people            name="謝XX" age="23" >aaaaaaaaa            <nationality>中國 </nationality>            <graduation>XXX大學</graduation>            <introduction name="介紹">謝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 = new ArrayList<People>();factory = DocumentBuilderFactory.newInstance();try {//慣例 取得document檔案執行個體的過程builder = factory.newDocumentBuilder();inputStream = XmlMainActivity.this.getResources().getAssets().open("test.xml");//以工程檔案下assets檔案夾為根目錄document = builder.parse(inputStream);  //取得根Element 以此列出所有節點NodeListElement 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();// NodeList childenodes=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 blocke.printStackTrace();}   根據粗俗的理解:DOM解析過程有點像樹遍曆過程,①取得根節點Element root =document.getDocumentElement(); ②取得所有節點列表NodeList nodes = root.getElementsByTagName_r("people");③取得第i個子節點Element peopleitem = (Element) nodes.item(i);④取得第i個子節點的Attribute資料、NodeValue資料⑤列出第i個子節點的"introduction"子節點NodeList childenodes=peopleitem.getElementsByTagName_r("introduction");⑥重複③④步驟;⑦列出第i個子節點的"nationality"子節點NodeList。⑧重複③④步驟;...⑨取得根目錄下第i+1個子節點Element peopleitem = (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層標籤,只要邏輯上處理得當。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.