我从SOAP Web服务获得以下响应,我需要解析它以在HTML页面中显示数据
<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>
以下是我用来检索<b:BusinessUnitId>
标记值的一段代码。函数的输入是resultXml
,它是上面的XML和我attributeName
传递的"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;
}
该代码在IE和Firefox中完美运行,但我似乎无法在Chrome,Opera或Safari中使用它。我已经尝试过各种各样的东西,让它在这3个浏览器中运行,但都失败了。
任何帮助将不胜感激!感谢。
答案 0 :(得分:1)
好吧,终于让它上班了。 IE和Firefox可以使用getElementsByTagName("b:PrivilegeId")
,但Chrome,Opera和Safari可以使用getElementsByTagName("Privilege")
。
所以,我在这里使用了这个很棒的库:http://jsfiddle.net/pupunzi/dnJNS/来检测浏览器并修改代码。现在它就像一个魅力!
我的代码现在如下所示:
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;
}