tags:

views:

285

answers:

3

Hello folks,

I am trying to use jquery for xml parsing.

Snippet of my XML is as follows

<ex>Some text here <it>some italic text</it> some more text <it>text more </it> text text <it>some more italic</it>go go</ex>

I want to get the text within and tags and display my content as follows

Some text here <i>some italic text</i> some more text <i>text more</i> text text <i> some more italic</i> go go

Basically, get the text from as well as and format it as shown above

Any help is appreciated

Thanks

+2  A: 

Here are a couple of good tutorials to get you started, complete with code samples:

http://blog.reindel.com/2007/09/24/jquery-and-xml-revisited/

http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery

Robert Harvey
A: 

You could try something like this, i'm unsure on the $this.text(), as this may ignore the markup tags you have in the xml as you can't use .html() method in xml documents. Should hopefully point you in the right direction. This also assumes you have multiple 'ex' tags.

$.get("youXML.xml",
   function(data){
    var parsedHTML = "<div>";

    //For each ex tag do this
    $(data).find("ex").each(function(){
     var $this = $(this);//The current ex tag we are iteration over
     parsedHTML += "<p>"  + $this.text() + "</p>";
    });
    parsedHTML += "</div>";

    $("#container").html(parsedHTML);
   });
Nooshu
Thanks for the reply , but what I wanted is to figure out the it tag and replace that with html italics tag - any pointers on how to replace them ? $this.text() would give the entire text in ex tag which is not what I really want
Sunil
I think this will do the job http://docs.jquery.com/Manipulation/replaceWith#content. Replace with <i> with <it> etc, may be a case of reading in the whole line of text and then performing it on the string and reinserting it back into the DOM.
Nooshu
Just reading the docs it looks like you would do something like $("it").replaceWith("<i>" + $(this).text() + "</i>"). You may have to add an .each() in there for multiple tags.
Nooshu
A: 

Check out the jParse the jQuery XML Parse Plugin jParse !!

Abdel Olakara