How to parse an XML response using jQuery

I am trying to parse the xml response using jQuery and just output the element to the page, but I have not been successful.

Below is the code that I use to answer and parse it.

$.ajax({
    url: UCMDBServiceUrl,
    type: "POST",
    dataType: "xml",
    data: soapMessage,
    success: UCMDBData,
    crossDomain: true,
    contentType: "text/xml; charset=\"utf-8\""
});
alert("Sent2");
return false;
}

function UCMDBData(xmlHttpRequest, status, msg)
{
     alert("Came back1");
     $(xmlHttpRequest.responseXML).find('tns:CIs').each(function()
     {
        alert("Came back2");
        $(this).find("ns0:CI").each(function()
        {
            alert("Came back3");
            $("#output").append($(this).find("ns0:ID").text());
        });
     });     
}

I get warnings for "Came back1", but it does not seem to go any further. Below is the XML response I'm trying to parse using my jquery code above. The text that I end up trying to return from the answer is in this element

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header />
    <soapenv:Body>
        <tns:getFilteredCIsByTypeResponse xmlns:ns0="http://schemas.hp.com/ucmdb/1/types" xmlns:ns1="http://schemas.hp.com/ucmdb/ui/1/types" xmlns:ns2="http://schemas.hp.com/ucmdb/1/types/query" xmlns:ns3="http://schemas.hp.com/ucmdb/1/types/props" xmlns:ns4="http://schemas.hp.com/ucmdb/1/types/classmodel" xmlns:ns5="http://schemas.hp.com/ucmdb/1/types/impact" xmlns:ns6="http://schemas.hp.com/ucmdb/1/types/update" xmlns:ns7="http://schemas.hp.com/ucmdb/discovery/1/types" xmlns:ns8="http://schemas.hp.com/ucmdb/1/types/history" xmlns:tns="http://schemas.hp.com/ucmdb/1/params/query">
            <tns:CIs>
                <ns0:CI>
                    <ns0:ID>4d030502995a00afd989d3aeca2c990c</ns0:ID>
                    <ns0:type>nt</ns0:type>
                    <ns0:props>
                        <ns0:strProps>
                            <ns0:strProp>
                                <ns0:name>name</ns0:name>
                                <ns0:value>prodoo</ns0:value>
                            </ns0:strProp>
                        </ns0:strProps>
                        <ns0:booleanProps>
                            <ns0:booleanProp>
                                <ns0:name>host_iscomplete</ns0:name>
                                <ns0:value>false</ns0:value>
                            </ns0:booleanProp>
                        </ns0:booleanProps>
                    </ns0:props>
                </ns0:CI>
            </tns:CIs>
            <tns:chunkInfo>
                <ns0:numberOfChunks>0</ns0:numberOfChunks>
                <ns0:chunksKey>
                    <ns0:key1 />
                    <ns0:key2 />
                </ns0:chunksKey>
            </tns:chunkInfo>
        </tns:getFilteredCIsByTypeResponse>
    </soapenv:Body>
</soapenv:Envelope>

So my question is how to parse the data correctly? I believe that the syntax of the code is correct, but I do not get the expected results. I would appreciate any help, thanks.

EDIT

I changed my code to the following as a suggestion, but still no luck:

$.ajax({
    url: UCMDBServiceUrl,
    type: "POST",
    dataType: "xml",
    data: soapMessage,
    success: UCMDBData,
    crossDomain: true,
    contentType: "text/xml;"
    });
alert("Sent2");
return false;
}

function UCMDBData(data, textStatus, jqXHR) {
    alert("Came back1");
    $(data).find('tns:CIs').each(function () {
        alert("Came back2");
        $(this).find("ns0:CI").each(function () {
            alert("Came back3");
            $("#output").append($(this).find("ns0:ID").text());
            document.AppServerForm.outputtext.value = document.AppServerForm.outputtext.value + "http://localhost:8080/ucmdb/cms/directAppletLogin.do?objectId=" + $(this).find('ns0:ID').text() +"&infopane=VISIBLE&navigation=true&cmd=ShowRelatedCIs&interfaceVersion=8.0.0&ApplicationMode=ITU&customerID=1&userName=admin&userPassword=admin";

    });
});

}

, , "Came back1", , - xml jquery. ?

+5
4

. : jQuery XML [@nodeName = tns: CIs].

, "@" jQuery 1.3. - :.find('tns \: CIs'), , (uri). , , . uri. jquery-xmlns .

+2

jQuery .

function UCMDBData(data, textStatus, jqXHR) {
    alert("Came back1");
    $(data).find('tns:CIs').each(function () {
        alert("Came back2");
        $(this).find("ns0:CI").each(function () {
            alert("Came back3");
            $("#output").append($(this).find("ns0:ID").text());
        });
    });
}

, $.ajax contentType contentType: "text/xml" , (, XML ).

. jQuery.ajax().

+1

Based on your comment, why do something crazy with jQuery? Just use javascript!

var open = '<ns0:ID>';
var close = '</ns0:ID>';

var start = obj.indexOf(open) + open.length;
var end = obj.indexOf(close);

var result = obj.slice(start, end);

Here's a jsfiddle that shows it in action.

+1
source

The correct syntax will probably be

success: function(xml) {
    $(xml).find('tns:CIs').each(function() {
    ......
0
source

All Articles