Mr Chris Posted October 3, 2007 Share Posted October 3, 2007 Hi Guys, I want to display an XML file in a php file on my site. Now here's my code that i'm using: <?php // Load the XML data from the specified file name. // The second argument (NULL) allows us to specify additional libxml parameters, // we don't need this so we'll leave it as NULL. The third argument however is // important as it informs simplexml to handle the first parameter as a file name // rather than a XML formatted string. $pFile = new SimpleXMLElement('new_feed.xml', null, true); // Now that we've loaded our XML file we can begin to parse it. // We know that a channel element should be available within the, // document so we begin by looping through each channel foreach ($pFile->channel as $pChild) { // Print our channel specific information, this should be // easy to understand, basically we're grabbing the // title, descripting and link nodes and outputting their values echo "<h1>" . $pChild->title . "</h1>\n"; echo "<p>\n"; echo $pChild->attributes . "<br />\n"; printf('Visit us at <a href="%s">%s</a><br />' . "\n", $pChild->link, $pChild->link); echo "</p>\n"; // Now we want to loop through the items inside this channel foreach ($pFile->channel->item as $pItem) { echo "<p>\n"; // If this item has child nodes as it should, // loop through them and print out the data foreach ($pItem->children() as $pChild) { // We can check the name of this node using the getName() method. // We can then use this information, to, for example, embolden // the title or format a link switch ($pChild->getName()) { case 'title': echo "<b>$pChild</b><br />\n"; break; case 'link': printf('<a href="%s>%s</a><br />' . "\n", $pChild, $pChild); break; default: echo nl2br($pChild) . "<br />\n"; break; } } echo "</p>\n"; } } ?> and here's the XML file i'm linking to: http://www.slougheaz.org/xml/new_feed.xml And i've tried grabbing two pieces of information from this file. The title and the attributes but I can't seem to get it to load as it brings back this error: http://www.slougheaz.org/xml/example2.php Any suggestions, as i'm totally stuck? Thanks Chris Link to comment https://forums.phpfreaks.com/topic/71708-display-xml-in-php/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.