Web Analytics Made Easy -
StatCounter a beginner's trying to figure out strings. - CodingForum

Announcement

Collapse
No announcement yet.

a beginner's trying to figure out strings.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • a beginner's trying to figure out strings.

    I've been trying to find the solution to pulling a url. Now I'm going to an rss feed as a string, to pull the bit I need (per a suggestion by oldpedant).

    I've gotten the beginning part of this down, but I'm not sure how to narrow down to the string I want. What I have right now only pulls the character position and i think it's getting confused by the <'s, ['s and /'s. I'm assuming I need to figure out how to escape out these characters, so it doesn't think it's code?

    Here's where I'm at:

    Code:
    <script>
    
    $.ajax({
        url: 'http://www.website.com/folder/rss.xml',
        type: 'GET',
        dataType: 'text',
        timeout: 1000,
        error: function(){
            alert('Error loading XML document');
        },
    
        success: function(xml){
          var test = xml.search("CDATA");
    
    alert(test);
    
        }
    });
    </script>

    update: I made a little progress using this:
    Code:
    function(xml){
    
    var mySplitResult = xml.split("CDATA");
          var test = xml.search("CDATA");
    
    alert(mySplitResult[1]);
    
        }
    Last edited by turpentyne; Aug 30, 2011, 06:33 PM.

  • #2
    I think this should be moved back to the main JS forum. After tyne explains what what he is really trying to do is parse some of the xml in that function(xml) and ties it into his prior posts.

    And turp: I *did* warn you it wouldn't be easy to parse that CDATA string!

    (His question has nothing whatsoever to do with jQuery. He just happens to be using jQuery to make the AJAX call for him instead of hand-coding it.)
    Be yourself. No one else is as qualified.

    Comment


    • #3
      I actually think I might've figured it out... I hope, anyway! The url I need is the very first one that appears in the xml string, so I searched for http, then I search for the file extension, .jpg. I added a couple spaces to clip/add what I needed and the alert gives me the url. Now I just need to use that info to use where I need it. Here's what I came up with:

      Code:
      <script>
      
      $.ajax({
          url: 'http://www.website.com/folder/rss.xml',
          type: 'GET',
          dataType: 'text',
          timeout: 1000,
          error: function(){
              alert('Error loading XML document');
          },
      
          success: function(xml){
      
      var mySplitResult = xml.split("img src=");
            var test = xml.search("img src=")+9;
            var test2 = xml.search(".jpg")+4;
      
      var cutout = xml.slice(test,test2);
      
      alert(cutout);
      
      
          }
      });
      </script>
      Last edited by turpentyne; Aug 30, 2011, 07:48 PM.

      Comment

      Working...
      X