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

7
  • May I know the reason why u want to convert string to xml Commented Jun 28, 2011 at 6:04
  • @nitinJS: I want to parse it to see if severity=="warning" or so and other checkings too Commented Jun 28, 2011 at 6:05
  • 1
    possible duplicate of Convert String to XML Document in JavaScript - simply extract the string, convert the < and > and dump it in a jQuery object Commented Jun 28, 2011 at 6:07
  • @Vivek: I want &lt; and &gt; to get converted into < and > respectively, so that it becomes xml object and i can parse it Commented Jun 28, 2011 at 6:08
  • as you writtenIt only alerts 280, 280 `, then what should it alert according to you? Commented Jun 28, 2011 at 6:09

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

7 Comments

whats your link for jsfiddle?
@mplungjan: Thanks I also happened to find an alternate solution, here is a link of mine take a look
@Abhishek: Please see update. Complete processing with in my opinion more consistent html processing
@mplungjan: I tried, but it does not produce results.. can you take a look at my updated question? Thanks
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
|
3

use jQuery.parseXML, to parse your xml

1 Comment

I tried this $.parseXML( xml ) may be I can replace all &lt; and &gt; with respective < and > and then it might work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.