android基礎知識09:xml檔案解析02 DOM

來源:互聯網
上載者:User

       本文主要講述android中xml的解析方式。

       android基礎知識09:xml檔案解析01 SAX

        android基礎知識09:xml檔案解析02 DOM

       android基礎知識09:xml檔案解析03 PULL

       在這一節中我們使用DOM方式來處理。

        DOM方式解析xml是先把xml文檔都讀到記憶體中,然後再用DOM API來訪問樹形結構,並擷取資料的,但是這樣一來,如果xml檔案很大呢?手機CPU處理能力當然不能與PC機器比,因此在處理效率方面就相對差了,當然這是對於其他方式處理xml文檔而言。

       那麼如何處理呢?
具體思路是:
*首先利用DocumentBuilderFactory建立一個DocumentBuilderFactory執行個體
*然後利用DocumentBuilderFactory建立DocumentBuilder
*然後載入XML文檔(Document),
* 然後擷取文檔的根結點(Element),
* 然後擷取根結點中所有子節點的列表(NodeList),
* 然後使用再擷取子節點列表中的需要讀取的結點。

下面我們就開始讀取xml文檔對象,並添加進List中:代碼如下:
我們這裡是使用assets中的river.xml檔案,那麼就需要讀取這個xml檔案,返回輸入資料流。
讀取方法為:inputStream=this.context.getResources().getAssets().open(fileName);   參數是xml檔案路徑,當然預設的是assets目錄為根目錄。
然後可以用DocumentBuilder對象的parse方法解析輸入資料流,並返回document對象,然後再遍曆doument對象的節點屬性。

public class DomXml {//擷取全部河流資料    /**     * 參數fileName:為xml文檔路徑     */    public List<River> getRiversFromXml(String fileName,Context context){        List<River> rivers=new ArrayList<River>();        DocumentBuilderFactory factory=null;        DocumentBuilder builder=null;        Document document=null;        InputStream inputStream=null;        //首先找到xml檔案        factory=DocumentBuilderFactory.newInstance();        try {            //找到xml,並載入文檔            builder=factory.newDocumentBuilder();            inputStream=context.getResources().getAssets().open(fileName);            document=builder.parse(inputStream);            //找到根Element             Element root=document.getDocumentElement();             NodeList nodes=root.getElementsByTagName(Contant.RIVER);            //遍曆根節點所有子節點,rivers 下所有river             River river=null;             for(int i=0;i<nodes.getLength();i++){                     river=new River();                      //擷取river元素節點                     Element riverElement=(Element)(nodes.item(i));                     //擷取river中name屬性值                     river.setName(riverElement.getAttribute(Contant.NAME));                     river.setLength(Integer.parseInt(riverElement.getAttribute(Contant.LENGTH)));                     //擷取river下introduction標籤                     Element introduction=(Element)riverElement.getElementsByTagName(Contant.INTRODUCTION).item(0);                     river.setIntroduction(introduction.getFirstChild().getNodeValue());                     Element imageUrl=(Element)riverElement.getElementsByTagName(Contant.IMAGEURL).item(0);                     river.setImageurl(imageUrl.getFirstChild().getNodeValue());                  rivers.add(river);             }        }catch (IOException e){            e.printStackTrace();        } catch (SAXException e) {            e.printStackTrace();        }         catch (ParserConfigurationException e) {            e.printStackTrace();        }finally{            try {                inputStream.close();            } catch (IOException e) {                    e.printStackTrace();            }        }        return rivers;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.