我的响应来自服务器端的xml格式(部分如下)
<list>
<Response>
<cfgId>903</cfgId>
<recommendations>
<Rule>
<name>Env SSA_RB_RESTART</name>
<category>none</category>
<severity>warning</severity>
<ruleEvalResult>true</ruleEvalResult>
<actionResult>Setting SSA_RB_RESTART=0 will cause RuleBase corruption.Kindly unset this environment variable and
restart the IIR server</actionResult>
</Rule>
</recommendations>
</Response>
<Response>
<cfgId>903</cfgId>
<recommendations>
<Rule>
<name>SSAOPTS (+Ltd) setting</name>
<category>none</category>
<severity>warning</severity>
<ruleEvalResult>true</ruleEvalResult>
<actionResult>
Please note that it is not recommended to have SSAOPTS=+Ltd in case of the
Production environment.</actionResult>
</Rule>
</recommendations>
</Response>
</list>
更新
我使用Response
获取主xml=$(xmldata); var resp_x = $(xml).find('Response').text();
标记,然后我尝试使用以下替换
resp_x = resp_x.replace(/</g,"<");
resp_x = resp_x.replace(/>/g,">");
var rule_x = $(resp_x).find('name').text();
alert(rule_x);
但它让我空白,请帮我抓取severity
。
更新我的回答
var xmlString=xmlToString(xmldata);
xmlString=xmlString.replace(/(<)/g,"<").replace(/(>)/g,">");
xml=StringtoXML(xmlString);
//now I can do my operations here
$(xml).find('Response').each(function(){
console.debug($(this).find('severity').text());
});
function xmlToString(xmlObj) {
if (navigator.appName == "Netscape")
{
return (new XMLSerializer()).serializeToString(xmlObj);
}
if (navigator.appName == "Microsoft Internet Explorer")
{
return xmlObj.xml;
}
}
function StringtoXML(text){
if (window.ActiveXObject){
var doc=new ActiveXObject('Microsoft.XMLDOM');
doc.async='false';
doc.loadXML(text);
} else {
var parser=new DOMParser();
var doc=parser.parseFromString(text,'text/xml');
}
return doc;
}
答案 0 :(得分:3)
您过早地将返回的XML转换为jQuery对象,然后将替换错误。尝试这样的事情:
$.ajax({
url: 'list.xml',
dataType: 'text',
success: function(data) {
console.debug(data);
data = data.replace('<', '<', 'gm')
.replace('>', '>', 'gm');
console.debug(data);
var $severities = $(data).find('severity');
console.debug($severities);
}
});
在replace()
中,您需要g
(全局)和m
(多行)标记。有关详细信息,请参阅String.replace()上的文档。
答案 1 :(得分:1)
试试这个
resp_x = resp_x.replace(/(<)/g,"<");
resp_x = resp_x.replace(/(>)/g,">");
var rule_x=$(resp_x).find('name').text();
alert(rule_x);
希望这有帮助
答案 2 :(得分:1)
下一个例子可以帮到你:
var str="<Rule><name>Env SSA_RB_RESTART</name><category>none</category><severity>warning</severity><ruleEvalResult>true</ruleEvalResult><actionResult>Setting SSA_RB_RESTART=0 will cause RuleBase corruption.Kindly unset this environment variable and restart the IIR server</actionResult>"
str=str.replace(/<|>/g,function(s){return s==="<"?"<":">"});
// str now is: "<Rule><name>Env SSA_RB_RESTART</name><category>none</category><severity>warning</severity><ruleEvalResult>true</ruleEvalResult><actionResult>Setting SSA_RB_RESTART=0 will cause RuleBase corruption.Kindly unset this environment variable and restart the IIR server</actionResult>"
<强>更新强>
下一个代码无需替换<
和>
来获取severity
:
var response="<list><Response><cfgId>903</cfgId><recommendations><Rule><name>Env SSA_RB_RESTART</name><category>none</category><severity>warning</severity><ruleEvalResult>true</ruleEvalResult><actionResult>Setting SSA_RB_RESTART=0 will cause RuleBase corruption.Kindly unset this environment variable and restart the IIR server</actionResult> </Rule></recommendations></Response><Response><cfgId>903</cfgId><recommendations><Rule><name>SSAOPTS (+Ltd) setting</name><category>none</category><severity>warning</severity><ruleEvalResult>true</ruleEvalResult><actionResult> Please note that it is not recommended to have SSAOPTS=+Ltd in case of the Production environment.</actionResult></Rule></recommendations></Response></list>";
var recs=$(response).find("recommendations");
for(var i=0;i<recs.length;i++) {
var xml=$("<recommendations>"+$(recs[i]).text()+"</recommendations>");
alert(xml.find("severity").text());
}