5

I've some XML which i need to show in a div as text.
Can we convert this XML to format like below.

<root>
<field>
  <label>Have you invested before</label>
  <value>No</value>
</field>
<field>
  <label>Are you looking to invest in the next 6 months</label>
  <value>Maybe</value>
</field>
<field>
  <label>What investments are you interested in</label>
  <value>Carbon Credits, Green Investments</value>
</field>
<field>
  <label>How much are you looking to invest</label>
  <value>£50,000 -  £100,000</value>
</field>
</root>

Output should be like as below:

Have you invested before : No
Are you looking to Invest in the next 6 months : Maybe
What Investments are you interested in : Carbon Credits,Green Investments
How much are you looking to invest : £50,000 - £100,000

Is this possible using Jquery/javascript.??

And it should be rendering like below HTML.

<div class="how-to">
<div class="how-text">
    <h3>Your Requirements</h3>
        <ul class="requirements">         
<li><label class="field-l">Have you invested before:</label> <span>No</span></li>         
<li><label class="field-l">Are you looking to Invest in the next 6 months:
</label>      <span>Maybe</span></li>         
<li><label class="field-l">What Investments are you interested in:</label> 
<span>Carbon  Credits,Green Investments</span></li>
 <li><label class="field-l">How much are you looking to invest:</label>
  <span>£50,000 -  £100,000</span></li>
      <li class="link-pad"><a href="index.html" class="requirements">
     Change  your requirements</a></li>
    </ul>
<div class="clear"></div>
 </div>
  </div>
4
  • Alright, but whatever that string is I'm generating this using some dynamic controls its some sort of Jquery form builder..with the help of that i can render it as HTML controls..but i need to show it as text as well along with controls...I'm unable to do that..that's why i asked for help..currently i'm no where with the code.. Commented Apr 19, 2013 at 20:53
  • 2
    Your XML is not well formed. You cannot have spaces in your tags otherwise they are attributes and need to have a value. Commented Apr 19, 2013 at 21:27
  • @MayankPathak I writed an example to you. As Austin says, the main problem of your question is it isn't valid xml. Please edit the question for future references. Commented Apr 20, 2013 at 10:31
  • Edited as per your answer.. ;) thanks Commented Apr 22, 2013 at 9:33

1 Answer 1

27

Step 1: validate your xml

Your xml is not valid. You can check if it's valid or not, for example, in an online validator. There are lots of them, this one I have linked is only an example.

For this answer, let's suppose we have some xml as follows:

<root>
  <field>
      <label>Have you invested before</label>
      <value>No</value>
  </field>
  <field>
      <label>Are you looking to invest in the next 6 months</label>
      <value>Maybe</value>
  </field>
  <field>
      <label>What investments are you interested in</label>
      <value>Carbon Credits, Green Investments</value>
  </field>
  <field>
      <label>How much are you looking to invest</label>
      <value>£50,000 -  £100,000</value>
  </field>
</root>

Step2: get the xml, maybe through ajax

I suppose this is obvious but I will include it here anyway.

$.get('/url_of_the_xml_resource')
  .done(function(data){
    // this function is executed if the request was sucessfull
  })
  .fail(function(){
    // this function is executed if the request fails
  })
;

Step 3: parse the xml

You can use jQuery's $.parseXML to parse it and convert the raw data into a XML document.

$.get('/url_of_the_xml_resource')
  .done(function(data){
    // parse the xml
    data = $.parseXML(data);
    //
    // do anything you want with the parsed data
  })
  .fail(function(){
    alert('something went wrong!');
  })
;

Step 4: play with the data

Now we have a xml document to play with. The following snippet assumes we have a definition list, <dl> tag, in our HTML layout, and is supposed to be executed after the data is parsed, like in the previous step.

// first we query the HTML document to get the list element
// and store it for later use
var list = $('dl');
// data is a xml document now, so we query it...
$(data)
  // and search for all <field> elements
  .find('field')
  // now we can play with each <field>
  .each(function(index, element){
    // as example we query & store the field
    var field = $(element)
    // get the values we want
    var label = field.find('label').text()
    var value = field.find('value').text()
    // and append some html in the <dl> element we stored previously
    list
      .append('<dt>'+label+': </dt>')
      .append('<dd>'+value+'</dd>')
    ;
  })
;

Conclusion

jQuery is what you want to use. Its chainable nature makes transversing the DOM like cutting butter. I hope this answer to be useful for you.

As an additional reference, see the full example on jsfiddle

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

4 Comments

Care to add some meat to this answer?
Wow. This is one of the most complete answers I've seen on SO (not counting Jon Skeet's).
Much, much better. Well written!
Thanks laconbass..I generated XML this way and it worked for me..thanks mate.. :)

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.