Question

I am getting the following response from a SOAP webservice, which I need to parse for displaying data in a HTML page

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Body>
    <ExecuteResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">
      <ExecuteResult i:type="b:RetrieveUserPrivilegesResponse" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:b="http://schemas.microsoft.com/crm/2011/Contracts">
        <a:ResponseName>RetrieveUserPrivileges</a:ResponseName>
        <a:Results xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
          <a:KeyValuePairOfstringanyType>
            <c:key>RolePrivileges</c:key>
            <c:value i:type="b:ArrayOfRolePrivilege">
              <b:RolePrivilege>
                <b:BusinessUnitId>e09f3de8-925d-e311-8a5d-000c299c86a3</b:BusinessUnitId>
                <b:Depth>Global</b:Depth>
                <b:PrivilegeId>b7a1f52a-b8a9-11d8-84ab-0007e9e100cc</b:PrivilegeId>
              </b:RolePrivilege>
              <b:RolePrivilege>
                <b:BusinessUnitId>e09f3de8-925d-e311-8a5d-000c299c86a3</b:BusinessUnitId>
                <b:Depth>Global</b:Depth>
                <b:PrivilegeId>655e4282-b8a9-11d8-84ab-0007e9e100cc</b:PrivilegeId>
              </b:RolePrivilege>
              <b:RolePrivilege>
                <b:BusinessUnitId>e09f3de8-925d-e311-8a5d-000c299c86a3</b:BusinessUnitId>
                <b:Depth>Global</b:Depth>
                <b:PrivilegeId>98b5ccb2-b8a9-11d8-84ab-0007e9e100cc</b:PrivilegeId>
              </b:RolePrivilege>
            </c:value>
          </a:KeyValuePairOfstringanyType>
        </a:Results>
      </ExecuteResult>
    </ExecuteResponse>
  </s:Body>
</s:Envelope>

Following is the piece of code I am using to retrieve the <b:BusinessUnitId> tag values. The input to the functions are resultXml which is the XML above and attributeName which I am passing as "b:PrivilegeId".

function extractValuesFromXml(resultXml, attributeName)
{
    var valueArray = new Array();

    var parseXml;

    if (typeof window.DOMParser != "undefined") 
    {
        parseXml = function(xmlStr) {
            return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
        };
    } 
    else if (typeof window.ActiveXObject != "undefined" &&  new window.ActiveXObject("Microsoft.XMLDOM")) 
    {
        parseXml = function(xmlStr) {
            var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(xmlStr);
            return xmlDoc;
        };
    } 
    else 
    {
        throw new Error("No XML parser found");
    }

    var xmlDoc = parseXml(resultXml);

    x = xmlDoc.getElementsByTagName(attributeName);

    for (i = 0; i < x.length; i++)
    {
        if (x[i].childNodes[0] != null && x[i].childNodes[0] != 'undefined' && x[i].childNodes[0].data != null && x[i].childNodes[0].data != 'undefined')
        {
            valueArray[i] = x[i].childNodes[0].data.toUpperCase();;
        }
    }

    return valueArray;  
}

The code works perfectly in IE and Firefox, but I can't seem to get it to work in Chrome, Opera or Safari. I have tried all sorts of things to have this working in these 3 browsers but have failed.

Any help would be appreciated! Thanks.

Était-ce utile?

La solution

Well, finally got it to work. IE and Firefox could use the getElementsByTagName("b:PrivilegeId") but Chrome, Opera and Safari could use getElementsByTagName("Privilege").

So, I used this wonderful library here : http://jsfiddle.net/pupunzi/dnJNS/ to detect the browser and modify the code. And now it works like a charm!

My code now looks like the following:

function extractValuesFromXml(resultXml)
{
    var valueArray = new Array();

    var parseXml;

    if (typeof window.DOMParser != "undefined") 
    {
        parseXml = function(xmlStr) {
            return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
        };
    } 
    else if (typeof window.ActiveXObject != "undefined" &&  new window.ActiveXObject("Microsoft.XMLDOM")) 
    {
        parseXml = function(xmlStr) {
            var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(xmlStr);
            return xmlDoc;
        };
    } 
    else 
    {
        throw new Error("No XML parser found");
    }

    var xmlDoc = parseXml(resultXml);

    if($.browser.msie == true || $.browser.mozilla == true) 
    {
        x = xmlDoc.getElementsByTagName("b:PrivilegeId");
    }
    else
    {
        x = xmlDoc.getElementsByTagName("PrivilegeId");
    }


    for (i = 0; i < x.length; i++)
    {
        if (x[i].childNodes[0] != null && x[i].childNodes[0] != 'undefined' && x[i].childNodes[0].data != null && x[i].childNodes[0].data != 'undefined')
        {
            valueArray[i] = x[i].childNodes[0].data.toUpperCase();;
        }
    }

    return valueArray;  
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top