XML第三講

來源:互聯網
上載者:User

XML解析

一、        XML解析方式分為  dom  sax 兩種

a)      實現原理

                       i.             Dom  實現的原理是整個xml文檔一次性獨處,放在一個屬性結構裡,在需要的時候,尋找特定借點,然後對借點進行讀或寫。   優 實現簡單。 缺 比較占記憶體

                     ii.             Sax 只在xml文檔中尋找特定條件的內容,並且只提取需要的內容,這樣做占記憶體小,靈活,它的缺點是就是寫。只能讀取,不能寫。

b)     XML解析器  Crimson、Xerces、Aelfred2

c)      開發套件  Jaxp Jdpmdpm4j

                       i.             Jaxp  由javax。xml    org.w3c.dom    org.xml.sax 包及子包組成

                     ii.             編寫java程式完成xml文檔的解析  dom解析器  用javax.xml.parsers包下的類來建立對象。

1.      DocumentBuilder的對象是dom解析器 通過對應的工廠類建立        DocumentBuilderFactory. Factory=DocumentBuilderFactory.newInstance();

DocumentBuilderdb=factory.newDocumentBuilder();

對xml文檔進行相應的解析

匯入包 or。w3c、dom

C

Db.parse(new File(”src/book.xm”l));

//可以用反射機制來實現擷取DocumentBuilder

解析內部的內容

Xml文檔它裡面所有的內容都會被當成 Node類型處理。  Element元素節點 Text文本節點,attr 屬性節點

Org.w3c.dom   Node類。 處理節點  節點類型

appendChild(Node newCharild)添加到此末尾一個新的節點

getNodeValue() 元素節點為空白。 文本節點為文本

getNodeName() 得到該節點的名稱 元素節點返回名稱

getNodeType() 得到該節點的類型

案例代碼:

//產生一個原廠模式對象

DocumentBuilderFactory.Factory=DocumentBuilderFactory.newInstance();

產生解析器對象

DocumentBuilderdb=factory.newDocumentBuilder();

對xml進行解析

Document doc =Db.parse(newFile(”src/book.xm”l));

//輸出文檔對象的子節點的個數  共有一個節點,根節點

findCharildren(doc); //Node是Document的介面

//System.out.println(Doc.getChildNodes().getLength());

//擷取他的第一個子節點

//Noderoot=doc.getChildNodes().item(0);

//輸入根節點的名字類型和值

//System.out.println(root.getNodeName+”   ”  //root.getNodeType()+”  ”+root.getNodeValue());

 

Public static voidfindCharildren(Document doc){

//先通過文檔對象doc得到根節點對象

Node root=NodeDoc.getElement ByTagName(“書架”).item(0);//根據名字尋找節點,返回第一個 根節點

//通過root對象得到根節點的所有兒子節點,返回一個節點的集合

NodeListlist=root.getChildNodes();

System.out.println(list.length());//輸出子節點個數

For(inti=0;i<list.length();i++){

Node n=list.item(i);

System.out.println(n.getNodeName()+”   ”+n.getNodeType()+”   ”+n.getNodeValue());

}

}

                     //定義方法,找到指定元素中子項目的名字相同的第二個元子素

                     Public static voidfindFixedChild(Node node){

                     NodeListlist=node.getChildNodes();

                     Int count=0;

For(int i=0;i<list.length();i++){

                            Boolean flag=false;

Node n1=list.item(i);

                            If(n1.getNodeName().equals(“書”)){

                                   Flag=true;

                                   Count++;

}

If(flag && count==2){

       NodeListlist1=n1.getChildNodes();

       For(intj=0;j<list1.length();j++){

              Noden2=list1.item(j);

              System.out.println(n2.getNodeName()+”  ”+n2.getNodeType()+”  ”+n2.getNodeValue());

}

}

}

}

                     //遍曆屬性節點

                     Public static voidfindAttibutes(Node node){

                            NameNodeMap map=node.getAttributes();

                            If(map!=null){

                                   For(inti<0;i<map.getLength();i++){

                                          Noded=map.item(i);

                                          System.out.println(d.getNodeName()+”   ”+d.getNodeType()+”   ”+d.getNodeValue());

}

      

}

 

}

2.      SAXParsers的對象是sax解析器 通過對象的工廠類建立

聯繫我們

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