2

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() );
Xavier Delamotte
  • 3,519
  • 19
  • 30
deadendstreet
  • 111
  • 1
  • 9

2 Answers2

3

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.

Community
  • 1
  • 1
alexcasalboni
  • 1,726
  • 1
  • 16
  • 26
0

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>
Francois Borgies
  • 2,378
  • 31
  • 38
  • Thanks actually what I started with. I just don't understand how to update the var xml to link to an actual file. In that example, the xml is part of the document. Is it possible to link to a file, say in a folder on my server? – deadendstreet Mar 29 '13 at 15:14