我正在尝试使用jQuery解析一个xml响应,并只输出一个页面元素,但我没有成功。
下面是我用来响应和解析它的代码。
$.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());
});
});
}
我收到了"Came back1“的警报,但似乎不会再继续下去了。下面是我试图使用上面的jquery代码解析的XML响应。我最终试图从响应中返回的文本在以下元素中
<?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>
所以我的问题是,我如何正确地解析数据?我相信代码语法是正确的,但是我没有得到预期的返回结果。如果有任何帮助,我将不胜感激,谢谢。
编辑
我已经将我的代码修改为下面的建议,但仍然没有成功:
$.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";
});
});
}
当我执行时,我收到的唯一警告消息是“来了back1”,这意味着代码仍然不能正确地使用jquery。还有其他建议吗?
发布于 2011-11-30 07:14:27
名称空间作用域的名称需要稍有不同地处理。根据这个答案:jQuery XML parsing with namespaces,你需要使用一个属性选择器@nodeName=tns:CIs。
对于高于1.3的jQuery版本,您可能需要去掉"@“。另一个建议是转义冒号:.find('tns\:CIs'),这很麻烦,因为它将语法前缀与语义名称空间( uri)合并在一起。所以如果前缀改变了,这个方法就会中断。更正确的答案将识别前缀到命名空间uri的映射。在这方面,jquery-xmlns plugin for namespace-aware selectors看起来很有前途。
发布于 2011-11-30 06:54:21
您的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() documentation。
发布于 2011-11-30 07:28:01
根据你的评论,为什么要对jQuery做一些疯狂的事情呢?只要使用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);
这是一个实际演示它的jsfiddle。
https://stackoverflow.com/questions/8318858
复制相似问题