如何更换& lt;和& gt;从客户端的xml响应中 [英] How to replace < and > from the xml response in client side
问题描述
我有一个来自服务器端的xml格式的响应(部分如下所示)
I have a response that comes from server side in xml format (partial like below)
<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>
更新
我用xml=$(xmldata); var resp_x = $(xml).find('Response').text();
获得主要的Response
标记,然后我尝试用下面的标记替换
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);
但是它给我空白,请帮助我获取severity
.
but it gives me blank, please help me fetch 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;
}
推荐答案
您太早将返回的XML转换为jQuery对象,然后执行了替换错误.尝试这样的事情:
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);
}
});
在replace()
中,您同时需要g
(全局)和m
(多行)标志.有关详细信息,请参见 String.replace()上的文档.
In the replace()
, you need both the g
(global) and m
(multi-line) flags. See the documentation on String.replace() for details.
这篇关于如何更换& lt;和& gt;从客户端的xml响应中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!