0

I am having a some problems in getting a node value from a XML file

My XML looks like this:

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

And I am trying to get 'test2' from Item2 > Description.

I am able to get the xml file display in a alert messagebox but can't seem to get the value i am looking for.

I am trying to do this in JavaScript and so far I have come up with the following:

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();
}

If I remove:

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

and change the alert to:

alert(xmlDoc)

it alerts my XML file, so i know it's reading my xml file but can't get the value.

Am I doing something wrong or is there a better way to get this value?

(I don't want to use jQuery for this)

1

2 Answers 2

2

Use xmlhttp.responseXML instead of xmlhttp.responseText, I think there might be some issue with older versions of IE though, in which case you can try

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(xmlhttp.responseText); 
Sign up to request clarification or add additional context in comments.

1 Comment

Soon as I changed it to responseXML it worked. Something so simple.
0

This function you can use to parse XML and use this link (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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.