开发者

jQuery read new XML depending on first XML

开发者 https://www.devze.com 2023-02-08 03:35 出处:网络
I have Working jQuery code which works at http://fiddle.jshell.net/gaby/UC2dM/2/show/ and reads an XML.

I have Working jQuery code which works at http://fiddle.jshell.net/gaby/UC2dM/2/show/ and reads an XML.

Now what i need to do is when someone clicks on News, Articles, Destinations (as an example) it should read the respective XML and their data.

For example, News xml would be "data_96.xml" Articles would read "data_97.xml" and so on...

the Generalised format of data_catid.xml looks like

<record>
    <recordId>251</recordId>
    <title>Czech Sandstone Chalk Ban Lifted</title>
    <author>|</author>
    <published>2010-01-20T14:36:00.000-08:00</published>
    <origUrl>http://www.rockandice.com/news/358-Czech-Sandstone-Chalk-Ban-Lifted</origUrl>
    <numComments>0</numComments>
    <data>&lt;![CDATA[&lt;p&gt;According to a report on czechclimbin</data>
</record>

please let me know how should i change the Jquery code to read the xmls..


Here is my Jquery Code

<script type='text/javascript'>
  //<![CDATA[ 
  $(window).load(function(){

  $.ajax({
    url:'data.xml',
    dataType: 'xml',
    type:'post',
    success: function(data){
        var xml = $(data);
        $('#container').append( CategoryToUl(xml.children()) );

    }
});

function CategoryToUl(开发者_JAVA技巧xml){
    var categories = xml.children('category');
    if (categories.length > 0)
    {
        var ul = $('<ul/>');
        categories.each(function(){
            var $this = $(this);
            var li = $('<li/>');
            var a = $('<a/>',{
                text: $this.children('title').text(),
                href: '#' + $this.children('catId').text()
            });
            li.append(a);
            li.append( CategoryToUl( $this ) );
            ul.append(li);
        });
        return ul;
    }
    return null;
}

  });
  //]]> 
  </script>

</head>
<body>
  <div id="container"></div>

</body>


You would just add something like this:

<script type='text/javascript'>
  //<![CDATA[ 
  $(window).load(function(){

$('a').live('click', function() {
    var catId = $(this).attr('href').replace('#', '');    
    //here you can use $.get to get your new xml file. 
    //construct the url using the catid
    //you would then parse the xml to display it.
});

//here is the rest of your code


You can make change in the CategoryToUl method, at the link generation to

var a = $('<a/>',{
    text: $this.children('title').text(),
    href: '#',
    'data-id': $this.children('catId').text(),
    class='categoryrecord'
});

and under $(window).load(function(){ add

$('a.categoryrecord').live('click',function(e){
   e.preventDefault();
   var cid = $(this).data('id');
   $.ajax({
      url:'data_' + cid + '.xml',
      dataType: 'xml',
      type:'post',
      success: function(data){
          var xml = $(data);
          // .. do what you want here with the xml
      }
   });
});
0

精彩评论

暂无评论...
验证码 换一张
取 消