deadendstreet
deadendstreet

Reputation: 111

Using JQuery to parse XML

I'm trying to use JQuery to parse XML that way I can post content from an RSS Feed to my site. However, I'm stuck.I'm not sure how to load a XML file with JQuery. I've created an example at the link below. Thanks

http://jsfiddle.net/deadendstreet/DytGM/1/

var xml;
$.get("http://straight2jackie.blogspot.com//feeds/posts/default?alt=rss", function(data) {
    xml = data;
});,
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find( "title" );

/* append "RSS Title" to #someElement */
$( "#someElement" ).append( $title.text() );

/* change the title to "XML Title" */
$title.text( "XML Title" );

/* append "XML Title" to #anotherElement */
$( "#anotherElement" ).append( $title.text() );

Upvotes: 2

Views: 7955

Answers (2)

alexcasalboni
alexcasalboni

Reputation: 1724

If you call $.get with the dataType parameter (i.e. dataType:'xml') you automatically have xml parsing.

By the way, I think here you're facing another problem: you can't load ajax resources from external (sub)domains: check here.

Upvotes: 3

Francois Borgies
Francois Borgies

Reputation: 2408

You can try the jQuery.parseXML() and a code something like :

<script>
    var xml = "<rss version='2.0'><channel><title>RSS Title</title></channel></rss>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $title = $xml.find( "title" );
    /* append "RSS Title" to #someElement */
    $( "#someElement" ).append( $title.text() );
    /* change the title to "XML Title" */
    $title.text( "XML Title" );
    /* append "XML Title" to #anotherElement */
    $( "#anotherElement" ).append( $title.text() );
</script>

Upvotes: 0

Related Questions