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

使用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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章