0

I have this XML

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<sizeResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<sizeReturn xsi:type="xsd:int">1</sizeReturn>
</sizeResponse>
</soapenv:Body>
</soapenv:Envelope>

I want to access the 1 but .find() is not working it's giving me this error in my console

Uncaught TypeError: Object <?xml version="1.0"
encoding="UTF-8"?><soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><sizeResponse
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><sizeReturn
xsi:type="xsd:int">0</sizeReturn></sizeResponse></soapenv:Body></soapenv:Envelope>
 has no method 'getElementsByTagName'

How can I access it otherwise using jQuery or JS (if there's a way using an Xpath plugin please provide the Xpath expression) ?

Thank you

2
  • I think this could help you: https://github.com/doedje/jquery.soap Commented May 14, 2013 at 5:38
  • 1
    That didn't deserve a downvote Commented May 14, 2013 at 7:43

2 Answers 2

1

Try this:

var xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><sizeResponse   soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><sizeReturn   xsi:type="xsd:int">0</sizeReturn></sizeResponse></soapenv:Body></soapenv:Envelope>',
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc );
console.log($xml.find("sizeReturn").html());

Read Docs http://api.jquery.com/jQuery.parseXML/

Fiddle: http://jsfiddle.net/cY5xZ/

Sign up to request clarification or add additional context in comments.

2 Comments

didn't work sorry man; look if that helps I have the XML string in the data variable of an Ajax call
@Seeking Knowledge: Perhaps you should give a little more context in your question (e.g the not working javascript part.)
1

Can you try this one.

<script>
    function GetVal()
    {
        var txt = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><sizeResponse   soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><sizeReturn   xsi:type="xsd:int">0</sizeReturn></sizeResponse></soapenv:Body></soapenv:Envelope>';
        if (window.DOMParser)
        {
            parser=new DOMParser();

            xmlDoc = parser.parseFromString(txt, "text/xml");

            var v = xmlDoc.getElementsByTagName("sizeReturn")[0].childNodes[0].nodeValue;
            alert("t" +v);
        }
        else // Internet Explorer
        {
            xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async=false;
            xmlDoc.loadXML(txt); 
        }
    }

</script>

Cheers :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.