使用JavaScript和MSXML對XML文檔進行訪問

來源:互聯網
上載者:User

使用JavaScript和MSXML對XML文檔進行訪問。

 XML文檔(type.xml):

<?xml version="1.0" encoding="UTF-8"?>
<type xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="type1.xsd">
 <student state="true">
  <info>
   <name>張三</name>
   <sex>男</sex>
  </info>
  <grede>
   <chinese>1</chinese>
   <math>119</math>
  </grede>
 </student>
 <teacher state="true">
  <name>李四</name>
  <sex>女</sex>
  <subject>數學</subject>
 </teacher>
</type>

JavaScript程式(type.html):

 1,使用JavaScript遍曆type.xml
  
<html >
   <head>
      <title>使用JavaScript遍曆XML文檔</title>
     
   </head>
  
   <body>
    <script type="text/javascript" language="JavaScript" charset="gb2312">
     //執行個體化一個DOM對象,表示一個XML文檔。文檔使用MSXML分析。
     var xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
     //載入xml檔案(使用MSXML分析文檔,以樹形結構儲存在記憶體中),使DOM對象引用該文檔。
     xmlDocument.load("type.xml");
    
     //擷取xmlDocument對象的根項目。
     var element = xmlDocument.documentElement;
     document.writeln("<p><strong>" + element.nodeName + "</strong></p><ui>");
   
   //擷取根節點下的子節點。
   for (i = 0; i < element.childNodes.length; i++ )
   {
    var curNode = element.childNodes.item(i);
    document.writeln("<li><strong>" + curNode.nodeName + "</strong></li>");
   }
     document.writeln("</ui>");
    
     //擷取根節點下第一個子節點下的子節點。
     var currentNode = element.firstChild;
      // var currentNode =  element.childNodes.item(0);
     document.writeln("<p><strong>" + currentNode.nodeName + "</strong></p><ui>");
   for (i = 0; i < currentNode.childNodes.length; i++ )
   {
    var curNode = currentNode.childNodes.item(i);
    document.writeln("<li><strong>" + curNode.nodeName + "</strong></li>");
   }
     document.writeln("</ui>");
    
   //擷取根節點下與第一個子節點同級節點  下的子節點。
   var SiblingNode = currentNode.nextSibling;
     document.writeln("<p><strong>" + SiblingNode.nodeName + "</strong></p><ui>");
   for (i = 0; i < SiblingNode.childNodes.length; i++ )
   {
    var curNode = SiblingNode.childNodes.item(i);
    document.writeln("<li><strong>" + curNode.nodeName + " : "+ curNode.firstChild.nodeValue  + "</strong></li> ");
   }
     document.writeln("</ui>");  
    
      //擷取根節點的第一個子節點下的子節點的節點
     var firstChildNode = currentNode.firstChild;
     document.writeln("<p><strong>" + firstChildNode.nodeName + "</strong></p><ui>");
   for (i = 0; i < firstChildNode.childNodes.length; i++ )
   {
    var curNode = firstChildNode.childNodes.item(i);
    document.writeln("<li><strong>" + curNode.nodeName + " : "+ curNode.firstChild.nodeValue  + "</strong></li>");
   }
     document.writeln("</ui>");
    
     //擷取根節點的第一個子節點下的子節點的節點。   
     var nextSiblingNode = firstChildNode.nextSibling;
     document.writeln("<p><strong>" + nextSiblingNode.nodeName + "</strong></p><ui>");
   for (i = 0; i < nextSiblingNode.childNodes.length; i++ )
   {
    var curNode = nextSiblingNode.childNodes.item(i);
    document.writeln("<li><strong>" + curNode.nodeName + " : "+ curNode.firstChild.nodeValue  + "</strong></li> ");
   }
     document.writeln("</ui>");

     </script>
   </body>
  
</html>

相關文章

聯繫我們

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