SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    USA
    Posts
    1,407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML newbie - have RSS feed in xpath, now how do I use the elements?

    I have the code below that displays something like this:
    XPathObject Object ( [type] => 1 [nodeset] => Array ( [0] => domtext Object ( [type] => 3 [name] => #text [content] => Most recent North Carolina, United States Resumes2Work.com Job Orders [0] => 8 [1] => 135730432 ) [1] => domtext Object ( [type] => 3 [name] => #text [content] => http://www.yourresumehere.com [0] => 9 [1] => 135666280 ) [2] => domtext Object ( [type] => 3...

    I am adding this feed into an existing PHP page and want to simply take the TITLE element and the LINK element and create:
    <a href="link">title</a>

    Any ideas? thank you for any insight and how to get started!
    PHP Code:
    function url_get_contents($url) {
               
    $url_info parse_url($url);
               
    $query  "GET " $url_info['path'] . " HTTP/1.0\r\n";
               
    $query .= "Host: " $url_info['host'] . "\r\n";
               
    $query .= "Accept: */*\r\n";
               
    $query .= "\r\n";
               
    $fp fsockopen($url_info['host'], 80);
               
    fputs($fp$query);
               while( !
    feof($fp) ) {
                   
    $content .= fread($fp2048);
               }
               
    fclose($fp);
               return 
    substr($contentstrpos($content"\r\n\r\n")+4);
            }
            
    $dom domxml_open_mem(url_get_contents("www.nowhere.com/rss/data.xml"));
            
    $xp $dom->xpath_new_context();
            
    print_r($xp->xpath_eval('//title/text() | //link/text()')); 
    Also, I want to limit the entires to the first 15 or so. I've tried appending:
    [position()<16]
    to the xpath definition without success - any ideas?

    =============================================
    I found this script in Kevin Yanks tutorial and both locally and on my server, it indicates:
    HTML Code:
    Error reading RSS data
    PHP Code:
    <?php 
    // Create an XML parser
    $xml_parser xml_parser_create();

    // Set the functions to handle opening and closing tags
    xml_set_element_handler($xml_parser"startElement""endElement");

    // Set the function to handle blocks of character data
    xml_set_character_data_handler($xml_parser"characterData");

    // Open the XML file for reading
    $fp fopen("http://www.sitepoint.com/rss.php","r")
           or die(
    "Error reading RSS data.");

    // Read the XML file 4KB at a time
    while ($data fread($fp4096))
       
    // Parse each 4KB chunk with the XML parser created above
       
    xml_parse($xml_parser$datafeof($fp))
           
    // Handle errors in parsing
           
    or die(sprintf("XML error: %s at line %d",  
               
    xml_error_string(xml_get_error_code($xml_parser)),  
               
    xml_get_current_line_number($xml_parser)));

    // Close the XML file
    fclose($fp);

    // Free up memory used by the XML parser
    xml_parser_free($xml_parser);

    ?>
    Last edited by WebDevGuy; Sep 27, 2005 at 16:38.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •