How to replace < and > from the xml response

2020-07-30 02:35发布

I have a response that comes from server side in xml format (partial like below)

<list>
    <Response>
        <cfgId>903</cfgId>
        <recommendations>
            &lt;Rule&gt;
            &lt;name&gt;Env SSA_RB_RESTART&lt;/name&gt;
            &lt;category&gt;none&lt;/category&gt;
            &lt;severity&gt;warning&lt;/severity&gt;
            &lt;ruleEvalResult&gt;true&lt;/ruleEvalResult&gt;
            &lt;actionResult&gt;Setting SSA_RB_RESTART=0 will cause RuleBase corruption.Kindly unset this environment variable and
            restart the IIR server&lt;/actionResult&gt;
            &lt;/Rule&gt;
        </recommendations>
    </Response>
    <Response>
        <cfgId>903</cfgId>
        <recommendations>
            &lt;Rule&gt;
            &lt;name&gt;SSAOPTS (+Ltd) setting&lt;/name&gt;
            &lt;category&gt;none&lt;/category&gt;
            &lt;severity&gt;warning&lt;/severity&gt;
            &lt;ruleEvalResult&gt;true&lt;/ruleEvalResult&gt;
            &lt;actionResult&gt;
            Please note that it is not recommended to have SSAOPTS=+Ltd in case of the 
            Production environment.&lt;/actionResult&gt;
            &lt;/Rule&gt;
        </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(/&lt;/g,"<");
resp_x = resp_x.replace(/&gt;/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(/(&lt;)/g,"<").replace(/(&gt;)/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;
}

3条回答
何必那么认真
2楼-- · 2020-07-30 03:20

Try like this

resp_x = resp_x.replace(/(&lt;)/g,"<");
                resp_x = resp_x.replace(/(&gt;)/g,">");
                var rule_x=$(resp_x).find('name').text();
                alert(rule_x);

Hope this helps

查看更多
家丑人穷心不美
3楼-- · 2020-07-30 03:20

Next example can help you:

var str="&lt;Rule&gt;&lt;name&gt;Env SSA_RB_RESTART&lt;/name&gt;&lt;category&gt;none&lt;/category&gt;&lt;severity&gt;warning&lt;/severity&gt;&lt;ruleEvalResult&gt;true&lt;/ruleEvalResult&gt;&lt;actionResult&gt;Setting SSA_RB_RESTART=0 will cause RuleBase corruption.Kindly unset this environment variable and restart the IIR server&lt;/actionResult&gt;"

str=str.replace(/&lt;|&gt;/g,function(s){return s==="&lt;"?"<":">"});
// 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 &lt; and &gt; for getting severity:

var response="<list><Response><cfgId>903</cfgId><recommendations>&lt;Rule&gt;&lt;name&gt;Env SSA_RB_RESTART&lt;/name&gt;&lt;category&gt;none&lt;/category&gt;&lt;severity&gt;warning&lt;/severity&gt;&lt;ruleEvalResult&gt;true&lt;/ruleEvalResult&gt;&lt;actionResult&gt;Setting SSA_RB_RESTART=0 will cause RuleBase corruption.Kindly unset this environment variable and restart the IIR server&lt;/actionResult&gt; &lt;/Rule&gt;</recommendations></Response><Response><cfgId>903</cfgId><recommendations>&lt;Rule&gt;&lt;name&gt;SSAOPTS (+Ltd) setting&lt;/name&gt;&lt;category&gt;none&lt;/category&gt;&lt;severity&gt;warning&lt;/severity&gt;&lt;ruleEvalResult&gt;true&lt;/ruleEvalResult&gt;&lt;actionResult&gt; Please note that it is not recommended to have SSAOPTS=+Ltd in case of the  Production environment.&lt;/actionResult&gt;&lt;/Rule&gt;</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/

查看更多
劫难
4楼-- · 2020-07-30 03:25

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('&lt;', '<', 'gm')
                   .replace('&gt;', '>', '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.

查看更多
登录 后发表回答