4

I looked at Convert String to XML Document in JavaScript but I could not found a solution in my case, can somebody help me in my situation.

I have a string like below, I want to convert it to an XML Object, How can I do that?

<list>
<Response>
<cfgId>280</cfgId>
<recommendations>&lt;Rule&gt;
&lt;name&gt;simple rule&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; Current value of maxfilesperproc is  32
increase it to 1024&lt;/actionResult&gt;
&lt;/Rule&gt;</recommendations>
</Response>
</list>

Readable version of above xml

<list>

<Response>
<cfgId>280</cfgId>

<recommendations>
<Rule> <name>simple rule</name> <category>none</category> <severity>warning</severity>     <ruleEvalResult>true</ruleEvalResult> <actionResult>Current value of maxfilesperproc is  32
increase it to 1024</actionResult> </Rule>

</recommendations>

</Response>
</list>

Updated, Here is what i tried.

var xml;
$.post("/csm/rules.action",
  { sessiontoken:   sessiontoken,
    cfgid:          cfgid},
      function(xmldata)
      {

            xml=$(xmldata);


      }
);
var htmlTable = $('<table></table>');

$(xml).find('Response').each(function(){
  var cid = $(this).find('cfgId').text();
  alert(cid+", "+cfgid);
  if(cid==cfgid) {   

    // Now grab the entitiy string
    var newXmlString = $(xml).find('recommendations').text(); 
    // Convert the entities to HTML and return a jQuery object
    var newXml = $("<div/>").html(newXmlString);

    // NOW we can get at the inner XML
    var ruleseverity=$(newXml).find('severity').text();
    if(ruleseverity=="warning")  {
      var rulename=$(newXml).find('name').text();
      var rulecategory=$(newXml).find('category').text();
      var ruleresult=$(newXml).find('ruleEvalResult').text();
      var ruleactionresult=$(newXml).find('actionResult').text();
        htmlTable.append('<tr><td>RuleName:'+rulename+'</td><td>RuleResult: '+ruleactionresult+'</td></tr>');
    }           
  }
});

I am adding htmlTable later in the code '<div class="block">'+htmlTable+'</div>'

I does not alerts at all

Community
  • 1
  • 1
AabinGunz
  • 12,109
  • 54
  • 146
  • 218
  • May I know the reason why u want to convert string to xml – Nitin Sawant Jun 28 '11 at 06:04
  • @nitinJS: I want to parse it to see if `severity=="warning"` or so and other checkings too – AabinGunz Jun 28 '11 at 06:05
  • 1
    possible duplicate of [Convert String to XML Document in JavaScript](http://stackoverflow.com/questions/1290321/convert-string-to-xml-document-in-javascript) - simply extract the string, convert the < and > and dump it in a jQuery object – mplungjan Jun 28 '11 at 06:07
  • @Vivek: I want `<` and `>` to get converted into `<` and `>` respectively, so that it becomes xml object and i can parse it – AabinGunz Jun 28 '11 at 06:08
  • `as you written`It only alerts 280, 280 `, then what should it alert according to you? – Vivek Jun 28 '11 at 06:09
  • @mplungjan: I mentioned about this link in my question, I tried it and then i posted my question, as you can see `xml=$(xmldata);` I followed the link – AabinGunz Jun 28 '11 at 06:10
  • @Abhishek - yes. I see it now. SO's links are not very obvious on single small words – mplungjan Jun 28 '11 at 06:23

2 Answers2

5

Although it is a possible duplicate of Convert String to XML Document in JavaScript - we can use a little help from jquery decode html entities

I made a fiddle

// the $() creates a jQuery object of the outer XML
var xml = $('<list><Response><cfgId>280</cfgId><recommendations>&lt;Rule&gt;&lt;name&gt;simple rule&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; Current value of maxfilesperproc is  32 increase it to 1024&lt;/actionResult&gt;&lt;/Rule&gt;</recommendations></Response></list>');

UPDATE This is more correct:

http://jsfiddle.net/mplungjan/ppj3nquL/

var xmlString = '<list><Response><cfgId>280</cfgId><recommendations>&lt;Rule&gt;&lt;name&gt;simple rule&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; Current value of maxfilesperproc is  32 increase it to 1024&lt;/actionResult&gt;&lt;/Rule&gt;</recommendations></Response></list>';
var xmlDocument = $.parseXML(xmlString);
var $xml = $(xmlDocument);


var cfgid = 280;
var htmlTable = $('<table></table>');

$xml.find('Response').each(function() {
  var cid = $(this).find('cfgId').text();
  if (cid == cfgid) {
    // Now grab the entitiy string
    var newXmlString = $(this).find('recommendations').text();
    // Convert the entities to HTML and return a jQuery object
    var newXml = $("<div/>").html(newXmlString);

    // NOW we can get at the inner XML
    var ruleseverity = $(newXml).find('severity').text();
    if (ruleseverity == "warning") {
      var rulename = $(newXml).find('name').text();
      var rulecategory = $(newXml).find('category').text();
      var ruleresult = $(newXml).find('ruleEvalResult').text();
      var ruleactionresult = $(newXml).find('actionResult').text();
      htmlTable.append('<tr><td>RuleName:' + rulename + '</td><td>RuleResult: ' + ruleactionresult + '</td></tr>');
    }
  }
});
$("#container").append(htmlTable);
td {
  border: 1px solid black
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container"></div>
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • whats your link for jsfiddle? – AabinGunz Jun 28 '11 at 06:16
  • @mplungjan: Thanks I also happened to find an alternate solution, here is a [link](http://jsfiddle.net/rPLCR/1/) of mine take a look – AabinGunz Jun 28 '11 at 06:23
  • @Abhishek: Please see update. Complete processing with in my opinion more consistent html processing – mplungjan Jun 28 '11 at 06:34
  • @mplungjan: I tried, but it does not produce results.. can you take a look at my updated question? Thanks – AabinGunz Jun 28 '11 at 06:49
  • Why not use my methods? Look at the code, I grab the newXml from the entity string. You have to FIRST convert the outer XML, THEN convert the inner XML – mplungjan Jun 28 '11 at 06:50
  • @mplungjan: I totally copied your code, but it does not even shows the alert `alert(cid+", "+cfgid);` I must be doing something wrong – AabinGunz Jun 28 '11 at 07:01
  • This is not the correct answer in terms of parsing XML. The `$()` syntax parses the `` parameter as HTML. Not at all the same. The correct way to parse XML is to use `jQuery.parseXML()` as noted by Vivek. – Alexis Wilke Jan 29 '15 at 07:13
3

use jQuery.parseXML, to parse your xml

Vivek
  • 10,978
  • 14
  • 48
  • 66
  • I tried this `$.parseXML( xml )` may be I can replace all `<` and `>` with respective `<` and `>` and then it might work – AabinGunz Jun 28 '11 at 06:14