簡體   English   中英

從XML文件中獲取價值使用JavaScript

[英]Get value from XML file Using JavaScript

我在從XML文件中獲取節點值時遇到了一些問題

我的XML看起來像這樣:

<item>
   <item1><Description>test</Description></item1>
   <item2><Description>test2</Description></item2>
   <item3><Description>test3</Description></item3>
</item>

我試圖從Item2> Description獲得'test2'。

我能夠在警告消息框中顯示xml文件,但似乎無法獲得我正在尋找的值。

我試圖用JavaScript做到這一點,到目前為止,我已經提出了以下內容:

function get_item()
{
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }


      xmlhttp.onreadystatechange=function()
       {

       if (xmlhttp.status==200)
         {
         //alert(xmlhttp.responseText);
          xmlDoc = xmlhttp.responseText;

            var item = xmlDoc.getElementsByTagName("Description")[0]; 
                item = item.childNodes.length ? item.childNodes[0].nodeValue : "" ;

             alert(item)

         } else
         {
         alert('Panel not communicating.Reason: '+xmlhttp.status);
         }
       }



    xmlhttp.open("POST","http://192.168.0.5/xml_file.xml",false);

    xmlhttp.send();
}

如果我刪除:

var item = xmlDoc.getElementsByTagName("Description")[0]; 
                    item = item.childNodes.length ? item.childNodes[0].nodeValue : "" ;

並將警報更改為:

alert(xmlDoc)

它提醒我的XML文件,所以我知道它正在讀取我的xml文件,但無法獲取值。

我做錯了什么還是有更好的方法來獲得這個價值?

(我不想為此使用jQuery)

使用xmlhttp.responseXML而不是xmlhttp.responseText ,我認為IE的舊版本可能存在一些問題,在這種情況下你可以試試

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xmlhttp.responseText); 

此函數可用於解析XML並使用此鏈接(http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript)。

    function readXML()
    {
       if(xmlDoc.readyState == 4)
       {
        //Using documentElement Properties
        //Output company
        alert("XML Root Tag Name: " + xmlDoc.documentElement.tagName);

        //Using firstChild Properties
        //Output year
        alert("First Child: " + xmlDoc.documentElement.childNodes[1].firstChild.tagName);

    //Using lastChild Properties
    //Output average
    alert("Last Child: " + xmlDoc.documentElement.childNodes[1].lastChild.tagName);

    //Using nodeValue and Attributes Properties
    //Here both the statement will return you the same result
    //Output 001
    alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes[0].nodeValue);
    alert("Node Value: " + xmlDoc.documentElement.childNodes[0].attributes.getNamedItem("id").nodeValue);

    //Using getElementByTagName Properties
    //Here both the statement will return you the same result
    //Output 2000
    alert("getElementsByTagName: " + xmlDoc.getElementsByTagName("year")[0].attributes.getNamedItem("id").nodeValue);

    //Using text Properties
    //Output John
    alert("Text Content for Employee Tag: " + xmlDoc.documentElement.childNodes[0].text);

    //Using hasChildNodes Properties
    //Output True
    alert("Checking Child Nodes: " + xmlDoc.documentElement.childNodes[0].hasChildNodes);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM