Information:

Extreme novice
PHP 4

Problem:

Trying to pull information from XML file and change it into a different layout and output to a new XML file. There seems to be many 'simpleXML' solutions, but this doesn't seem to be available on the server with PHP 4.

Here is the XML before:

<?xml version="1.0" encoding="ISO-8859-1" ?><?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/shared/bsp/xsl/rss/nolsol.xsl"?><rss version="2.0"><channel><title>BBC Sport | Football | Premiership | UK Edition</title><link>http://news.bbc.co.uk/go/rss/-/sport1/hi/football/eng_prem/default.stm</link><description>Visit BBC Sport for all the action as it happens - up-to-the-minute news, results, breaking news, video, audio and feature stories. BBC Sport covers the major events and all the professional football clubs in the UK, plus cricket, rugby, golf, tennis, motorsport and all the main world sports.</description><language>en-gb</language><lastBuildDate>Thu, 21 Sep 2006 09:01:35 GMT</lastBuildDate><copyright>Copyright: (C) British Broadcasting Corporation, see http://news.bbc.co.uk/sport1/hi/help/rss/4517815.stm for terms and conditions of reuse</copyright><docs>http://www.bbc.co.uk/syndication/</docs><ttl>15</ttl><image><title>BBC Sport</title><url>http://news.bbc.co.uk/sol/shared/img/sport_120x60.gif</url><link>http://news.bbc.co.uk/go/rss/-/sport1/hi/football/eng_prem/default.stm</link></image><item><title>Liverpool 2-0 Newcastle</title><description>Xabi Alonso scores a wonder goal and Dirk Kuyt gets his first for Liverpool as the Reds beat Newcastle.</description><link>http://news.bbc.co.uk/go/rss/-/sport1/hi/football/eng_prem/5351794.stm</link> <guid isPermaLink="false">http://news.bbc.co.uk/sport1/hi/football/eng_prem/5351794.stm</guid><pubDate>Wed, 20 Sep 2006 20:58:25 GMT</pubDate><category>Premiership</category></item><item><title>Pearce shrugs off pressure talk</title><description>Manchester City boss Stuart Pearce plays down job pressure as his side&apos;s dismal run continues.</description><link>http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/m/man_city/5366420.stm</link> <guid isPermaLink="false">http://news.bbc.co.uk/sport1/hi/football/teams/m/man_city/5366420.stm</guid><pubDate>Thu, 21 Sep 2006 08:44:09 GMT</pubDate><category>Man City</category></item></channel></rss>

And here is some php code courtesy of miniXML I tried and some of it worked until I started trying to to pull the elements from the file and then got into a complete and utter mess:

/****Use the MiniXML library****/
require_once('minixml.inc.php');

/****Create a MiniXMLDoc object called footyDoc****/
$footyDoc = new MiniXMLDoc();

/****Put string from XML file into footyDoc****/
$footyDoc->fromFile('./footy.xml');

/****Start at root element of footyDoc****/
$rootEl =& $footyDoc->getRoot();

/****Verify and show what is in the footyDoc****/
print $footyDoc->toString();

/****Start getting the elements within the footyDoc XML file****/
$item =& $footyDoc->getElement('item');

/**** We can now use the ITEM element to get access to it's children.
$title =& $item->getElement('title');

$description =& $item->getElement('description');

$link =& $item->getElement('link');

/**************************************************
*I NOW NEED TO SORT THE ELEMENTS INTO A NEW LAYOUT AND POST THEM INTO A NEW XML FILE*
***************************************************/

Here is what I would like the final XML file to like like after I get the php code to work:

<items>
<item title="Liverpool 2-0 Newcastle" description="Xabi Alonso scores a wonder goal and Dirk Kuyt gets his first for Liverpool as the Reds beat Newcastle." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/eng_prem/5351794.stm" />

<item title="Pearce shrugs off pressure talk" description="Manchester City boss Stuart Pearce plays down job pressure as his side&apos;s dismal run continues." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/m/man_city/5366420.stm" />
</items>

Can anybody offer any advice why it's not working or suggest an easier method for changing the layout of the XML file. Sorry in advance if this has already done the rounds, but I have spent many hours searching these forums and enjoying the crash course on parsing XML files. Any advice or exmples would be appreciated in trying to resolve this issue.

Meantime I will continue my enlightenment by working on the second half of the php code and try to get the layout correct from the elements above.

    If you want to use SimpleXML, it is not available in PHP 4.x. Upgrade to PHP 5. Your better off using PHP 5 anyway as PHP 4 is no longer supported as of last year.

    You can also use the XSL module (http://us2.php.net/manual/en/book.xsl.php) but again, use it with PHP 5.

      Cheers kkobashi, I will continue to push my webspace provider to upgrade from php 4 to php 5. but in the meantime I'm enjoying the crash course and will take a look at your link.

      If anybody has got any suggestions for parsing and outputing in php 4 then this would be greatly appreaciated while I await my webspace provider to get there finger out. :-)

        Regarding PHP5: any host that has not yet upgraded is probably not a host worth using, as this means they've gone -- at a minimum -- over a year now without any possibility of having installed any security patches, since there haven't been any for PHP4.

        Anyway, if you're stuck with it and intend to stay there anyway, there's always the old [man]DOMXML[/man] extension. There are also a bunch of XML-related PEAR packages that might be of use.

          NogDog, cheers for the assistance,,,,I will take a look into the XML related PEAR packages and hopefully find something that may suit my needs.

          Good news guys, after mentioning the information NogDog said about the security patches, my website provider has agreed to move me onto a a newer server with PHP5 during this week.

          So I may well be visiting this area again and asking for help with php5 commands.

            23 days later

            Finally I'm on a new server providing PHP5. I have scrapped the miniXML above and tried simpleXML with XSL to get the XML file and transform it into what I'm after. But alas my efforts are wasted and I require some assistance.

            Please bear in mind that this is a hobby and I have very little programming experience.

            Here's my php (try not to laugh):

            <?php
            
            /*Fetch XML remotely*/
            $data = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_prem/rss.xml'); 
            if(!$data) 
            { 
                    die('Error reading RSS data.'); 
            }
            /*Load XML into SimpleXML string*/ 
            $sxe = simplexml_load_string($data);
            
            if ($sxe === false) {
               echo 'Error while parsing the document';
               exit;
            }
            
            /*Convert SimpleXML string into Dom object*/ 
            $dom_sxe = dom_import_simplexml($sxe);
            if (!$dom_sxe) {
              echo 'Error while converting XML';
              exit;
            }
            
            $dom = new DOMDocument('1.0');
            $dom_sxe = $dom->importNode($dom_sxe, true);
            $dom_sxe = $dom->appendChild($dom_sxe);
            
            /*XSLT transform*/
            $xslt = new XSLTProcessor(); 
            $XSL = new DOMDocument(); 
            $XSL->load( 'footy.xsl', LIBXML_NOCDATA); 
            $xslt->importStylesheet( $XSL ); 
            
            /*show output*/ 
            print $xslt->transformToXML( $dom );
            
            /*Save to new XML file*/ 
            echo $xslt->save('Footy2.xml');
            ?>
            

            Here's the XSL:

            <xsl:stylesheet version="1.0"  
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns="http://www.w3.org/1999/xhtml"> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"
            media-type="application/xhtml+xml" encoding="iso-8859-1"
            doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
            doctype-system=
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/> <xsl:template match="/channel">
            <html>
            <head><title>Premiership News</title></head>
            <body><xsl:apply-templates/></body>
            </html>
            </xsl:template> <xsl:template match="title">
            <b>title= </b><xsl:apply-templates/><br/>
            </xsl:template> <xsl:template match="description">
            <b>description= </b><xsl:apply-templates/><br/>
            </xsl:template> <xsl:template match="link">
            <b>url= </b><xsl:apply-templates/><br/>
            </xsl:template>
            </xsl:stylesheet>

            I know it won't transform it to what I want, but first I need to get it to work.
            Can anybody help? or provide a little advice for this novice.

              Does this help? If so, you're welcome 🙂

              <?php
              // Get the RSS feed and load it into a SimpleXML object
              $file = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_prem/rss.xml');
              $xml  = simplexml_load_string($file);
              
              // Create a new instance of domdocument
              $dom  = new DOMDocument('1.0', 'iso-8859-1');
              $dom->preserveWhiteSpace = false;
              $dom->formatOutput = true;
              // Set the root element and the sub element 'items'
              $root  = $dom->createElement('rss');
              $entry = $dom->createElement('items');
              $entry = $root->appendChild($entry);
              
              // Pull all of the titles, descriptions, and links from $file
              foreach ($xml->channel->item as $item) {
                $title = $item->title;
                $desc  = $item->description;
                $url   = $item->link;
              
                $element = $dom->createElement('item');
                $element->setAttribute('title', $title);
                $element->setAttribute('description', $desc);
                $element->setAttribute('url', $url);
                $element = $entry->appendChild($element);
              }
              
              // Build a new XML file
              $dom->appendChild($root);
              $xml = $dom->saveXML();
              if ($dom->loadXML($xml)) {
                $dom->save('new_file.xml');
              }
              
              // Uncomment the line below to see what the XML looks like in a browser
              // echo header("Content-type: text/plain") . $dom->saveXML();
              //
              

                I would also have gone with jaql's approach, but I thought this was a good opportunity to have a look at XSL. I do not have access to XSLTProcessor, so I simply ran the XML through Firefox instead. From the code you posted above, I suspect you'd need to remove the stylesheet from the XML and use your original approach in this regard.

                First off, I'm curious to why you're first using simplexml_load_string, and then dom_import_simplexml. Why not just

                $dom = new DOMDocument();
                $dom->load($data);
                

                In the XML you load, there is allready an included XSL (first element after the opening xml tag). I don't know wether this has any impact on XSLTProcessor or not, but I chose to remove it since the result is treated as an RSS feed by Firefox, and view source shows the untransformed XML.

                Furhtermore, that XML uses the media namespace in <media:thumbnail>, without first declaring the namespace. Perhaps it's part of the RSS specification, but I made some modifications to the original XML and declared it myself.

                Since you are specifying a doctype, and also make use of <head><title>...</title></head>, I assumed that you want the document to be treated as an XHTML document and not XML. Else the data of the head element would not affect the title bar of a browser, but rather show up as a plain text. As such, I changed method="xml" to method="html".

                Finally, since you have several item elements, I used xsl:for-each.

                You should easily be able to make any necessary modifactions to the following code to match your needs.

                <?php
                header('Content-type: application/xml; charset=ISO-8859-1');
                
                $external = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_prem/rss.xml');
                // simply for visual inspection purposes. makes it easier to read the xml code
                $external = str_replace("><", ">\n<", $external);
                
                // remove everything before the first <title> element
                $start = strpos($external, '<title>');
                // remove everything from, and including,  the closing tag </rss>
                $length = strpos($external, '</rss>') - $start;
                $external = substr($external, $start, $length);
                
                /*	prepend the document with a new xml tag, your xsl and a new
                	channel element which also introduces the media namespace.
                	Since you use XSLTProcessor, you can probably remove the xml-stylesheet element.
                */
                $xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
                <?xml-stylesheet type="text/xsl" href="your_xsl_file.php"?>
                <channel xmlns:media="http://whatever.com/whatever">';
                
                echo $xml . $external;
                ?>
                

                And the XSL, included by the above code as your_xsl_file.php

                <?php
                	/*	Since you use XSLTProcessor, you should probably remove this php part
                		completely.
                	*/
                	header("Content-type: text/xsl; charset=ISO-8859-1");
                ?><?xml version="1.0" encoding="ISO-8859-1"?>
                <xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:output method="html" indent="yes" omit-xml-declaration="yes"  
                media-type="application/xhtml+xml" encoding="iso-8859-1"
                doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
                doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
                <xsl:template match="/channel"> <html>
                <head><title>Premiership News</title></head>
                <body> <items> <xsl:for-each select="item"> <div> <h2> <xsl:value-of select="title"/> </h2> <p> <xsl:value-of select="description"/> </p> </div> <br/> </xsl:for-each> </items> </body> </html> </xsl:template> </xsl:stylesheet>
                  5 days later

                  Jaql and johanafm - I bow down before the presence of the professionals. Once again, experience prevails amongst the inexperienced newbie like myself. These solutions are spot on and I have tested both to destruction but still have a few questions.

                  Jaql

                  Your solution is perfect and exactly what I'm looking for and will probably use this after johnafm comments about picking it. Also, I was wondering if I could change the php so that it outputs the new XML file without the

                  $root = $dom->createElement('rss');

                  root element, so that it's more like a txt file?

                  Johnafm

                  Thankyou for the crash course on XSL, it was just what the doctor ordered and will give me another alternative to what I would like to acomplish with my website. As for the :

                  First off, I'm curious to why you're first using simplexml_load_string, and then dom_import_simplexml. Why not just

                  It was all part of the learning curve and I didn't realise you could do that until mentioned. It's my 1st attempt at trying to write php with XSL to change the layout and is probably just a schoolboy error and lack of experience.

                  I was hoping that by saving time I could get the XML to output the changed layout to screen via XHTML for approval. Once approved remove the screen output and then produce it to a text file for actionscript to pull into flash.

                  So my new question to you, would be:

                  Can I edit the PHP you provided to output the information to a txt file?

                  The reasoning behind this, is that the txt file would be over written daily (refreshed) and all the information contained within it will be pulled into my Flash website and displayed in a very pretty format. 😃

                    BarmyArmy;10932195 wrote:

                    Can I edit the PHP you provided to output the information to a txt file?

                    So you just want a file, for example data.txt, filled with information like this?

                    <items>
                        <item title="Manager Ferguson admits FA charge" description="Manchester United manager Sir Alex Ferguson admits a charge of improper conduct for his comments on referee Alan Wiley's fitness." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/m/man_utd/8314326.stm"/>
                        <item title="Spitting on pitch 'is flu risk'" description="Bolton reveal four players have recovered from swine flu while their Carling Cup opponents Chelsea are unconcerned about claims that Blackburn players could have passed on the virus.
                     " url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/c/chelsea/8327576.stm"/>
                        <item title="Kroenke increases Arsenal holding" description="Arsenal director Stan Kroenke buys more shares in the London football club, but has not reached the threshold to trigger a takeover." url="http://news.bbc.co.uk/go/rss/-/1/hi/business/8328011.stm"/>
                        <item title="Drogba wants Chelsea goal record" description="Ivory Coast captain Didier Drogba hopes to stay at Chelsea long enough to break the club's goal-scoring record." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/africa/8328379.stm"/>
                        <item title="Blackburn trio contract swine flu" description="Three members of Sam Allardyce's Blackburn squad are ill with swine flu ahead of Tuesday's Carling Cup tie against Peterborough." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/b/blackburn_rovers/8325606.stm"/>
                        <item title="Spurs aim for new stadium by 2012" description="Tottenham Hotspur have submitted a planning application for their new 56,000-seat stadium and hope to be playing at the venue by 2012." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/t/tottenham_hotspur/8326735.stm"/>
                        <item title="Redknapp reveals Sandro interest" description="Tottenham manager Harry Redknapp has revealed his interest in signing Brazilian midfielder Sandro." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/t/tottenham_hotspur/8327164.stm"/>
                        <item title="Robinho will not force Barca move" description="Manchester City forward Robinho is pleased about being linked with a move to Barcelona but insists he is not engineering a return to Spain." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/m/man_city/8326317.stm"/>
                        <item title="King 'broke woman's nose'" description="A Premier League footballer punched a woman in the face after she rejected his sexual advances in a bar, a court hears." url="http://news.bbc.co.uk/go/rss/-/1/hi/england/london/8326381.stm"/>
                        <item title="Higginbotham eager to take chance" description="Stoke City defender Danny Higginbotham hopes a good display against Portsmouth in the Carling Cup will put him back in contention for a first-team place." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/s/stoke_city/8327280.stm"/>
                        <item title="Benitez hails quality of Torres" description="Liverpool boss Rafael Benitez praises Fernando Torres after the striker played through the pain barrier in the 2-0 win over Manchester United." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/l/liverpool/8325882.stm"/>
                        <item title="Wigan suffer Figueroa injury blow" description="Wigan defender Maynor Figueroa faces several weeks on the sidelines after damaging knee ligaments during the 3-1 win at Burnley on Saturday." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/w/wigan_athletic/8325855.stm"/>
                        <item title="West Ham in photo appeal to fans" description="West Ham issue photos of 67 supporters they want to identify after crowd trouble which marred their Carling Cup match against Millwall." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/league_cup/8327332.stm"/>
                        <item title="Bellamy rues pitch invader clash" description="Manchester City striker Craig Bellamy regrets his run-in with a pitch invader at Old Trafford in September." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/teams/m/man_city/8325259.stm"/>
                        <item title="Wenger laments 'sickening' draw " description="Arsenal boss Arsene Wenger is left frustrated after watching his side let a lead slip for the second time in six days as they are held 2-2 by West Ham." url="http://news.bbc.co.uk/go/rss/-/sport1/hi/football/eng_prem/8325236.stm"/>
                      </items>
                    

                    Or do you actually need an XML file?

                    If you need an XML file, you'll need it to have a root element. It doesn't have to be 'rss,' but it does have to be something. It could probably be items instead. So then you're XML file would look something like:

                    <?xml version="1.0" encoding="iso-8859-1"?>
                    <items>
                       <item>...</item>
                       <item>...</item>
                       <item>...</item>
                    </items>
                    

                    If this is the case, here's the code to do that:

                    <?php
                    // Get the RSS feed and load it into a SimpleXML object
                    $file = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_prem/rss.xml');
                    $xml  = simplexml_load_string($file);
                    
                    // Create a new instance of domdocument
                    $dom  = new DOMDocument('1.0', 'iso-8859-1');
                    $dom->preserveWhiteSpace = false;
                    $dom->formatOutput = true;
                    // Set the root element
                    $root = $dom->createElement('items');
                    
                    // Pull all of the titles, descriptions, and links from $file
                    foreach ($xml->channel->item as $item) {
                      $title = $item->title;
                      $desc  = $item->description;
                      $url   = $item->link;
                    
                      $element = $dom->createElement('item');
                      $element->setAttribute('title', $title);
                      $element->setAttribute('description', $desc);
                      $element->setAttribute('url', $url);
                      $element = $root->appendChild($element);
                    }
                    
                    // Build a new XML file
                    $dom->appendChild($root);
                    $xml = $dom->saveXML();
                    if ($dom->loadXML($xml)) {
                      $dom->save('new_file.xml');
                    }
                    
                    // Uncomment the line below to see what the XML looks like in a browser
                    // echo header("Content-type: text/plain") . $dom->saveXML(); 
                    //
                    

                      You could maybe do something like this to just create a text file (that doesn't look so pretty) that contains <item> elements:

                      // Get the RSS feed and load it into a SimpleXML object
                      $file = file_get_contents('http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_prem/rss.xml');
                      $xml  = simplexml_load_string($file);
                      $name = 'rss_data.txt';
                      if (($handle = fopen($name, 'a')) === FALSE) {
                        die('Failed to open ' . $name);
                      }
                      
                      // Pull all of the titles, descriptions, and links from $file
                      foreach ($xml->channel->item as $item) {
                        $title = $item->title;
                        $desc  = $item->description;
                        $url   = $item->link;
                      
                        $element  = "<item title='$title' ";
                        $element .= "description='$desc' ";
                        $element .= "url='$url'/>\n";
                        fwrite($handle, $element);
                      }
                      fclose($handle);
                      

                      This will create a file called rss_data.txt (if it doesn't already exist) and append new content to it. If you wanted to rewrite the file each time the script is executed, you'd just change the 'a' option to 'w' like so:

                      $handle = fopen($name, 'w')
                        johanafm;10931737 wrote:

                        From the code you posted above, I suspect you'd need to remove the stylesheet from the XML and use your original approach in this regard.

                        What I meant here was that you should use your original approach with XSLTProcessor to get the transformed XML rather than my approach with the XSL embedded in the XML (which I chose since I only had Firefox to use for the XSL transformation).

                        So, to actually get the transformed XML into a file, you'd start by removing the php part from my XSL file, i.e. remove the following

                        <?php
                            /*    Since you use XSLTProcessor, you should probably remove this php part
                                completely.
                            */
                            header("Content-type: text/xsl; charset=ISO-8859-1");
                        ?>
                        

                        I also made some changes to the original XML, including the following 3 lines. Remove the second line: <?xml-stylesheet ...?> since that's handled by your XSLTProcessor instead

                        $xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
                        <?xml-stylesheet type="text/xsl" href="your_xsl_file.php"?>
                        <channel xmlns:media="http://whatever.com/whatever">';
                        

                        And with the above changes to my code, your original code ought to do the job with a minor change, i.e.

                        /*XSLT transform*/
                        $xslt = new XSLTProcessor();
                        $XSL = new DOMDocument();
                        // where footy.xsl is the xsl I provided after the change described above
                        $XSL->load( 'footy.xsl', LIBXML_NOCDATA);
                        $xslt->importStylesheet( $XSL );
                        
                        // Change this line from output 
                        // print $xslt->transformToXML( $dom ); 
                        
                        // to saving into (and overwriting) a file instead
                        file_put_contents('transformed.xml', $xslt->transformToXML($dom));
                        

                        And finally, as you've allready noticed, I didn't actually write the XSL to match your original specification. You'd need to make some changes in it to make it create an XML instead of an XHTML document. But you've got a working foundation to start from, and I believe the above should make it work with XSLT Processor.

                          6 days later

                          jaql & johanafm

                          Thank you for the updates, I have been a little busy this week, but intend on testing the code over next weekend and will give you some feedback from the results and maybe some more questions. 🙂

                            9 days later

                            jaql:

                            I have tested the php and it works like a charm. I have a couple of hiccups where strange characters seem to appear randomly, but I think this may be the orignal RSS feed sending html tags in the information.

                            I have also adapted the same php to create a new txt file with a different layout which bring me onto these next couple of questions.

                            I would love to be able to pull down images from a XML file and store them locally on the server and post them into a txt document as previously.

                            XML code

                            <item>
                            <title>Saw III</title>
                            <link>http://movies.yahoo.com/shop?d=hv&amp;cf=trailer&amp;id=1809278202&amp;intl=us</link>
                            <description>With his new apprentice Amanda (Shawnee Smith), Jigsaw (Tobin Bell) has once again eluded capture and vanished. While city detectives scramble to locate him, Doctor Lynn Denlon (Bahar Soomekh) is kidnapped and taken to an abandoned warehouse where she meets bedridden and on the verge of death. She is told that she must keep the madman alive for as long as it takes Jeff (Angus Macfayden), another of his victims, to complete a game of his own. Racing against the ticking clock of Jigsaw's own heartbeat, Lynn and Jeff struggle to make it through each of their vicious tests, unaware that he has a much bigger plan for both of them.</description>
                            <pubDate>Thu, 21 Sep 2006 07:04:26 GMT</pubDate>
                            <media:category>movie/trailers</media:category>
                            <media:credit role="Producer">Mark Burg</media:credit>
                            <media:title>Saw III</media:title>
                            <media:player url="http://us.rd.yahoo.com/movies/trailers/1809278202/1809741644/?http://movies.yahoo.com/shop?d=hv&amp;cf=mf_frame&amp;id=1809278202&amp;mspid=1809741644&amp;mfurl=http://mediaframe.yahoo.com/launch?lid=wmv-700-p.1468177-169904,wmv-1000-p.1468178-169904,wmv-56-p.1468174-169904,wmv-100-p.1468175-169904,wmv-300-p.1468176-169904&amp;p=movies&amp;f=1809278202&amp;.spid=1809741644&amp;.dist=Lionsgate%20Films&amp;type=t"/>
                            <guid isPermaLink="false">1809278202</guid>
                            <media:thumbnail height="74" url="http://us.ent3.yimg.com/movies.yahoo.com/images/hv/photo/movie_pix/lions_gate_films/saw_iii/sawiii_teaserposter2-th.jpg" width="50"/>
                            </item>
                            

                            Can I pull the images from:

                            and place them in a folder on the server and also reference them in the output from the php code shown below:

                            // Get the RSS feed and load it into a SimpleXML object 
                            $file = file_get_contents('http://rss.ent.yahoo.com/movies/top25trailers.xml'); 
                            $xml  = simplexml_load_string($file); 
                            
                            // Create a new instance of domdocument 
                            $dom  = new DOMDocument('1.0', 'iso-8859-1'); 
                            $dom->preserveWhiteSpace = false; 
                            $dom->formatOutput = true; 
                            // Set the root element 
                            $root = $dom->createElement('thumbnails'); 
                            
                            // Pull all of the titles, descriptions, and links from $file 
                            foreach ($xml->channel->item as $item) { 
                              $title = $item->title; 
                              $url   = $item->link;
                              $desc  = $item->description;
                              $thumb = $thumb->thumbnail;
                            
                              $element = $dom->createElement('thumbnail'); 
                              $element->setAttribute('filename', $thumb);
                              $element->setAttribute('url', $url); 
                              $element->setAttribute('title', $title); 
                              $element->setAttribute('description', $desc); 
                              $element = $root->appendChild($element); 
                            } 
                            
                            // Build a new XML file 
                            $dom->appendChild($root); 
                            $xml = $dom->saveXML(); 
                            if ($dom->loadXML($xml)) { 
                              $dom->save('trailers.xml'); 
                            } 
                            
                            // Uncomment the line below to see what the XML looks like in a browser 
                            //echo header("Content-type: text/plain") . $dom->saveXML();
                            

                            I'm hoping the final result, will be a txt document that looks like this:

                            Final txt document

                            <?xml version="1.0" encoding="iso-8859-1"?>
                            <thumbnails>
                              <thumbnail filename="######.jpg" url="http://www.movie trailer.com title="movie title" description="movie description."/>
                            </thumbnails>
                            

                            Where the #####.jpg is the name of the image pulled from the RSS feed and saved on the local server.

                            Any advice on this would be greatly appreciated.

                              jaql:

                              Thank you for the prompt reply, I will take a look into the link and see what experience I can gain from it.

                              Using this php:

                              // Get the RSS feed and load it into a SimpleXML object 
                              $file = file_get_contents('http://www.movietrailers.com/trailers.xml'); 
                              $xml  = simplexml_load_string($file); 
                              
                              // Create a new instance of domdocument 
                              $dom  = new DOMDocument('1.0', 'iso-8859-1'); 
                              $dom->preserveWhiteSpace = false; 
                              $dom->formatOutput = true; 
                              // Set the root element 
                              $root = $dom->createElement('thumbnails'); 
                              
                              // Pull all of the titles, descriptions, and links from $file 
                              foreach ($xml->channel->item as $item) { 
                                $title = $item->title; 
                                $url   = $item->link; 
                                $desc  = $item->description; 
                                $thumb = $thumb->media:thumbnail; 
                              
                                $element = $dom->createElement('thumbnail'); 
                                $element->setAttribute('filename', $thumb); 
                                $element->setAttribute('url', $url); 
                                $element->setAttribute('title', $title); 
                                $element->setAttribute('description', $desc); 
                                $element = $root->appendChild($element); 
                              } 
                              
                              // Build a new XML file 
                              $dom->appendChild($root); 
                              $xml = $dom->saveXML(); 
                              if ($dom->loadXML($xml)) { 
                                $dom->save('trailers.xml'); 
                              } 
                              
                              // Uncomment the line below to see what the XML looks like in a browser 
                              //echo header("Content-type: text/plain") . $dom->saveXML(); 
                              

                              On the XML side of things, is there any reason why I cannot get the php to fetch the '#####.jpg' from the feed:

                              <media:thumbnail height="74" url="http://www.trailers.com/#####.jpg" width="50"/>

                              And output it to look like below:

                              <?xml version="1.0" encoding="iso-8859-1"?>
                              <thumbnails>
                              <thumbnail filename="######.jpg" url="http://www.movie trailer.com title="movie title" description="movie description."/>
                              </thumbnails>

                                Please can somebody confirm that for me to get the attribute - #####.jpg from the node - media:thumbnail in the XML:

                                <media:thumbnail height="74" url="http://www.trailers.com/#####.jpg" width="50"/>

                                I will have to use XPATH....?

                                I feel another steep learning curve starting. :eek:

                                  7 days later

                                  Hooray...I seem to have gotten a little further after messing around with the php jaql provided earlier in the post. No need for XPATH yet but using the php below:

                                  <?php 
                                  // Get the RSS feed and load it into a SimpleXML object 
                                  $file = file_get_contents('http://rssfeedfromwebsite.xml'); 
                                  $xml  = simplexml_load_string($file); 
                                  
                                  // Create a new instance of domdocument 
                                  $dom  = new DOMDocument('1.0', 'iso-8859-1'); 
                                  $dom->preserveWhiteSpace = false; 
                                  $dom->formatOutput = true; 
                                  // Set the root element 
                                  $root = $dom->createElement('thumbnails'); 
                                  
                                  // Pull all of the titles, descriptions, and links from $file 
                                  foreach ($xml->channel->item as $item) { 
                                    $title  = $item->title;
                                    $url    = $item->link;
                                    $desc   = $item->description;
                                    $media  = $item->children('http://search.yahoo.com/mrss');
                                    $thumb  = $media->thumbnail->attributes();
                                    $img    = (string) $thumb['url'];
                                  
                                    $element = $dom->createElement('thumbnail');
                                    $element->setAttribute('filename', $img);
                                    $element->setAttribute('url', $url);
                                    $element->setAttribute('title', $title);
                                    $element->setAttribute('description', $desc);
                                    $element = $root->appendChild($element);
                                  } 
                                  
                                  // Build a new XML file 
                                  $dom->appendChild($root); 
                                  $xml = $dom->saveXML(); 
                                  if ($dom->loadXML($xml)) { 
                                    $dom->save('output.txt'); 
                                  } 
                                  
                                  ?>
                                  

                                  I can now get all the information I need and post it into a txt document. But I would appreciate any help from anybody if they know how we can edit the output to only pick the filename and not the whole link.

                                  Output now:

                                  <?xml version="1.0" encoding="iso-8859-1"?>
                                  <thumbnails>
                                  <thumbnail filename="http://www.unwantedinformation.com/unwanteddirectory/filename.jpg" url="http://www.webaddress.com" title="title of subject" description="description of subject"/>
                                  </thumbnails>

                                  Output I would like:

                                  <?xml version="1.0" encoding="iso-8859-1"?>
                                  <thumbnails>
                                  <thumbnail filename="filename.jpg" url="http://www.webaddress.com" title="title of subject" description="description of subject"/>
                                  </thumbnails>

                                  Has anybody got any ideas on how to remove the unwanted link and leave just the filename.jpg?

                                  Any help would be greatly appreciated.

                                     $img    = (string) $thumb['url'];
                                    

                                    then

                                    $img = explode('/', $img);
                                    $img = $img[count($img) - 1];
                                    
                                      Write a Reply...