DOM解析在java和javascript不同的寫法

來源:互聯網
上載者:User

總結在java中及javascript中使用DOM解析xml檔案的不同點

1.   產生DOM文檔對象的方法不同

a)     在javascript中,建立DOM文檔對象代碼如下:

//建立xml文檔

varxmlDoc = new ActiveXObject("Microsoft.xmldom");

//設定不支援非同步

xmlDoc.async= false;

//載入xml檔案

xmlDoc.load("demo03.xml");

b)     在java中,建立DOM文檔對象代碼如下:

//建立一個解析器工程對象

PublicDocument document;

DocumentBuilderFactory  factory =DocumentBuilderFactory.newInstance();

//建立一個解析器對象

DocumentBuilderbuilder = factory.newDocumentBuilder();

//建立一個xml檔案在記憶體的樹形節點模型

document=builder.parse(DomReaderXML.class.getClassLoader().getResourceAsStream("xml檔案"));

2.擷取根節點及屬性名稱以及屬性值的方法

c)     在javascript中擷取:

varroot = xmlDoc.documentElement//擷取根節點

varnode=xmlDoc.documentElement.childNodes[0];//根節點下面的節點

document.write("<br/>==="+node.baseName);//baseName,用於輸出節點標籤

//節點類型,其中1代表元素節點,2代表屬性節點,3代表文本節點

document.write("<br/>節點類型:"+node.nodeType+"<br/>");

document.write("根項目節點名稱:"+node.ownerDocument.documentElement.tagName+"<br/>");      

document.write("當前節點的屬性名稱:"+node.attributes[0].name+"<br/>");

document.write("當前節點的屬性值:"+node.attributes[0].value);

document.write("<br/>當前節點及其子節點所包含的文本"+node.text);

 

 

d)  在java中擷取:

將xml文檔中的資料轉換成集合對象

//擷取所有student節點集合

NodeListnodes = document.getElementsByTagName("student");

List<Student>stuList = new ArrayList<Student>();

         //採用for迴圈輸出節點的標籤名和屬性

         for(int i=0;i<nodes.getLength();i++){

             Student stu = new Student();

             //轉換成element元素對象

             Element element = (Element)nodes.item(i);

             //System.out.println(element.getNodeName());

             //屬性值

             String id = element.getAttribute("id");

             stu.setId(id);

             NodeList childs =element.getChildNodes();

             String name =document.getElementsByTagName("name").item(i).getFirstChild().getNodeValue();

             stu.setName(name);

             String  strAge =document.getElementsByTagName("age").item(i).getFirstChild().getNodeValue();

             stu.setAge(Integer.parseInt(strAge));

             String sex = document.getElementsByTagName("sex").item(i).getFirstChild().getNodeValue();

             stu.setSex(sex);

             stuList.add(stu);

         }

//載入完成之後,採用xml檔案的方式進行尋找結點

    NodeList nodelist = document.getElementsByTagName("student");

    for(int i =0; i<nodelist.getLength();i++){

     Elementstuelement = (Element) nodelist.item(i);

             //擷取屬性的值

             String idValue =stuelement.getAttribute("id");

             //System.out.println("id屬性值"+idValue);

             NodeList child =stuelement.getChildNodes();

             for(int j=0;j<child.getLength();j++){

                  if(child.item(j).getNodeType()==Node.ELEMENT_NODE){

                      //System.out.println(Node.ELEMENT_NODE);

                      System.out.println(child.item(j).getNodeType());

                      Node node = child.item(j);                 System.out.println(node.getNodeName()+"=="+node.getTextContent());

                  }

             }

聯繫我們

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