I have a response that comes from server side in xml format (partial like below)
<list>
<Response>
<cfgId>903</cfgId>
<recommendations>
<Rule>
<name&开发者_如何学C;gt;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>
update
I get the main Response
tag with xml=$(xmldata); var resp_x = $(xml).find('Response').text();
and then I am trying to replace with below
resp_x = resp_x.replace(/</g,"<");
resp_x = resp_x.replace(/>/g,">");
var rule_x = $(resp_x).find('name').text();
alert(rule_x);
but it gives me blank, please help me fetch severity
.
UPDATE with my answer
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;
}
You're converting the returned XML to a jQuery object too early and then doing the replace wrong. Try something like this:
$.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);
}
});
In the replace()
, you need both the g
(global) and m
(multi-line) flags. See the documentation on String.replace() for details.
Try like this
resp_x = resp_x.replace(/(<)/g,"<");
resp_x = resp_x.replace(/(>)/g,">");
var rule_x=$(resp_x).find('name').text();
alert(rule_x);
Hope this helps
Next example can help you:
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>"
Update:
next code does not need to replace <
and >
for getting 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());
}
http://jsfiddle.net/ZpYac/
精彩评论