Javascript+DOM訪問XML檔案中的資料

來源:互聯網
上載者:User
 1.產生XML檔案。
        在ADO裡,用Recordset.Save方法可以儲存查詢內容至指定XML檔案,這樣產生的檔案裡面有很多內容是關於資料表的屬性的,即<s:Schema></s:Schema>節點內容。我們需要的是<rs:data></rs:data>節點的內容。結構大體如下:Data.xml
        <xml ...>
        <s:Schema>
                ...
        </s:Schema>
        <rs:data>
                <z:row PositionID='1001' PositionName='fdsafsd' CompanyID='1' PMID='1001' />
                <z:row PositionID='1002' PositionName='.NET Developer' CompanyID='2' PMID='1002' />
        </rs:data>
        這裡的<z:row />即為一個資料行,下一步就是顯示這部分內容。
2.LoadXML檔案。
 <SCRIPT language=javascript>
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var currNode;
xmlDoc.async = false;
xmlDoc.load("Data.xml");
if (xmlDoc.parseError.errorCode != 0) {//如果開啟檔案出錯
   var myErr = xmlDoc.parseError;
   document.write("You have error " + myErr.reason);
}
else
{
 document.write("<hr size=1>");
   xmlDoc.setProperty("SelectionLanguage", "XPath");
   currNode = xmlDoc.documentElement.childNodes[1];//取得<rs:data>部分
   alert(currNode.childNodes[0].attributes[0].value);//測試結果

    for(i=0;i<currNode.childNodes.length;i++) //遍曆每個'資料行'
   {
  for(j=0;j<currNode.childNodes[i].attributes.length;j++)//遍曆行的每個屬性
  {
   document.write(currNode.childNodes[i].attributes[j].name + " : " + currNode.childNodes[i].attributes[j].value + "<BR>");
  }
document.write("<hr size=1>");
   }
}
</script>

相關文章

聯繫我們

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