Friday, March 12, 2010 8:43 | Contact Us | Advertise With Us | Submit Your Aritlces/Posts

Parsing XML: XML to HTML using PHP!

Posted by DomainFunk.com on Friday, July 31, 2009, 8:31
This news item was posted in How-To's, Scripting, Tutorials category and has 2 Comments so far.

Lately I just finished scripting a nice little system that randomizes and publishes content based on an XML feed provided by partner content sponsor. While doing some research for this, I came to know I wasn’t the lone one who wanted to install such a system. The web app, for which I wrote this system, runs a script every week creating new .html files from an XML content feed and keeps creating these plain .HTML files on the fly, in essence growing my content library every week :)

Hope this comes in hand to you guys out there looking for this.

The Simple Parser

I needed a simple parser which would not need XSLT and the size of the parser would remain small. Since the parser would be placed as a cron job, I needed it to be simple and without the complexities of XSLT. So here is part of the code that made the parser worked.

<?php

$doc = new DOMDocument(); //We create a new DOM object.
$doc->load(’samplefeed.xml’,LIBXML_DTDLOAD|LIBXML_DTDATTR); //load all elements of the XML feed. Using LIBXML_DTDLOAD we  load external subset and use LIBXML_DTDATTR to set the default DTD attributes, both are PHP Lib XML constants.
$contentitems = $doc->getElementsByTagName( “books” ); // we are now fetching the elements, and with this statement fetching all elements enclosed within the <books> tags.

foreach($contentitems as $contentitem)
{

$titles = $contentitem->getElementsByTagName( “title” );
$title = $titles->item(0)->nodeValue; //we are not taking the values from each element nodes.

$authors = $metadata->getElementsByTagName( “byline” );
$author = $authors->item(0)->nodeValue;

$ISBNS = $metadata->getElementsByTagName( “isbn” );
$ISBN = $ISBNS->item(0)->nodeValue;

//below we are starting an HTML file and using a PHP variable to hold it.

$html = ‘<html>

<!– html code can come in here –>’;

/*main article starts here */

$html = $html.$title.”<br/>”;
$html = $html.$author.”<br/>”;
$html = $html.$ISBN.”<br/>”;

$html =’
/*footer part of the html page */
</html>’;

$ourFileName = “feedarticles/”.trim($title).”.html”; //keeping the title the same as
$ourFileName = str_ireplace(”?”,”",$ourFileName); //removing characters that get attached during char-set conversions or shall we say non-conversions.
$ourFileName = str_ireplace(”_”,”",$ourFileName);
$ourFileName = str_ireplace(”:”,”",$ourFileName);
$ourFileName = str_ireplace(”;”,”",$ourFileName);
$ourFileName = str_ireplace(”‘”,”",$ourFileName);

echo $ourFileName.” creating the file “; //Just echoing the statement while creating the file.
$ourFileHandle = fopen($ourFileName, ‘w’) or die(”can’t open file”); //init the file handle
fwrite($ourFileHandle, $html);//write the file.
fclose($ourFileHandle);//close the file handle.

}

?>

The XML Page

The below is the SampleFeed.xml file, with the fields that will be parsed.

<NewDataSet _xmlns3a_xsi=”http://www.w3.org/2001/XMLSchema-instance”>

<Books>
<title>Java Server Pages</title>
<author>SAMS</author>
<ISBN>1234</ISBN>

<title>Logistics</title>
<author>Tata Mcgraw Hil</author>
<ISBN>5678</ISBN>

</books>
</NewDataSet>

Note

Albeit, there are better and newer ways of parsing XML using PHP, this was suitable for me, because its pretty simple code :) and it works wonders!

Resources

Here are both files zipped up, make sure the permissions are setup right for directories that you want to user write these files into. Here is the entire Simple XML Parser zip, that creates .html files from XML feeds.

Enjoyed it? Share it!:
  • Digg
  • Google Bookmarks
  • del.icio.us
  • TwitThis
  • Facebook
  • Technorati
  • StumbleUpon
  • E-mail this story to a friend!
  • LinkedIn
  • Live
  • Ma.gnolia
  • MySpace
  • Yahoo! Buzz
  • Yigg
  • Sphinn
  • Mixx
  • blogmarks
  • blogtercimlap
  • Book.mark.hu
  • co.mments
  • De.lirio.us
  • DotNetKicks
  • LinkaGoGo
  • NewsVine
  • Reddit
  • scuttle
  • Spurl
  • YahooMyWeb
You can leave a response, or trackback from your own site.

2 Responses to “Parsing XML: XML to HTML using PHP!”

  1. Dash
    2009.10.13 11:26

    My XML page coming from a live feed has a tag which contains html elements like
    how to parse such xml file…pl help  

  2. DomainFunk.com
    2009.10.13 14:27

    If you your feed has an HTML tag inserted what you can do is using PHPs strip_tags function or the reg replace function to remove all HTML before parsing the XML feed.. more info on this functions can be found here http://php.net

Leave a Reply