#native_company# #native_desc#
#native_cta#

PHP and XML: Using the expat functions Page 2

By Justin Grant
on July 30, 2000

Let’s look at the XML document that is parsed by the ‘Newsboy’ class.

mynews.xml

<?xml version="1.0" standalone="no"?>
<!DOCTYPE NewsBoy SYSTEM "NewsBoy.dtd">

<NewsBoy>

<story>
	<date>03/31/2000</date>
	<slug>Sooo Busy !</slug>
	<text>
	I haven't posted anything here for a while now as I have been busy with work (have to pay those bills!). <newline></newline>
	I have just finished a neat little script that stores a complete record set in a session variable after <newline></newline>
	doing an SQL query. The neat part is that an XML doc is stored in the session variable an when paging <newline></newline>
	through the results (often near 1000!) the script displays 50 results at a time from the XML doc in the <newline></newline>
	session variable instead of doing another query against the database. It takes a BIG load off of the <newline></newline>
	database server.
	</text>
</story>

<story>
	<date>03/25/2000</date>
	<slug>NewsBoy Class</slug>
	<text>
	Converted Newsboy to a PHP class to allow better abstraction (as far as PHP allows.)   <newline></newline>
	Guess that means this is version 0.02 ?!					       <newline></newline>
	Newsboy will have a section of it's own soon on how to use and customize the class.    <newline></newline>
	</text>
</story>

<story>
	<date>03/24/2000</date>
	<slug>NewsBoy is up!</slug>
	<text>
	I have just finished NewsBoy v0.01 !!! <newline></newline>
	It looks quite promising. You may ask, &quot;What the heck is it?!&quot;. <newline></newline>
	Well it's a simple news system for web-sites, written in PHP, that makes use of XML for <newline></newline>
	the news data format allowing easy updating and portability between platforms.
It uses the built in expat parser for Apache.
This is just the very first version and there will be loads of improvements as the project progresses.
</text> </story> <story> <date>03/24/2000</date> <slug>Romeo must Die</slug> <text> Saw a really cool movie today at Mann called 'Romeo must Die' <newline></newline> Nice fight scenes for a typical kung-fu movie with some 'Matrix' style effects. <newline></newline> One particular cool effect was the 'X-Ray Vision' effect that occured in various fight scenes. <newline></newline> The hero, played by Jet Li, strikes a bad guy and you can see the bone in his arm crack, in X-RAY vision. <newline></newline> There were some funny scenes too when Jet has to play American football with the bad guys. <newline></newline> The official website for the movie is &lt;A HREF='http://www.romeo-must-die.com'&gt; here &lt;/A&gt; <newline></newline> <newline></newline> </text> &lt;IMG SRC=&quot;http://a1996.g.akamaitech.net/7/1996/25/e586077a88e7a4/romeomustdie.net/images/image15.jpg&quot; WIDTH=300 &gt; </story> </newsboy>

Ok, if you aren’t familiar with XML documents then this might look a
little scary, it’s really not though. The first line is an XML declaration.
The ‘version’ attribute tells the parser that the document conforms to the 1.0 XML standard as defined by the W3C.
The ‘standalone’ option tells the program reading the XML that the document requires a DTD to validate the
XML document (in this case the DTD resides in a separate file called ‘NewsBoy.dtd’, as specified by the DOCTYPE declaration on the next line,
but it could be specified in the same file if you wanted it to be).
The DOCTYPE declaration points to the root element of the XML document, which in this case is the ‘NewsBoy’ element.
It also specifies the location of the DTD which exists in the same folder as the XML file itself.
Please note, I am not validating the XML document against the DTD I have created because expat cannot validate an XML document.
According to James Clark, the author of expat, the reason for this is in conformance with the W3C’s specification for XML parsers, in
that the parser need not validate the document but the programmer should deal with that.
The remainder of the document includes stories that are built with tags that I have defined in my NewsBoy class.