Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

PHP and XML: using expat functions
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.
[ Next Page ]

[Page 1]  [Page 2]  


Comments:
RE: Atributes pleaseShubhangi07/24/07 06:05
RE: Sending XML request via SSL?smita06/27/07 16:12
to obtain a window with XML_NITFEliza04/26/07 14:10
Explanationanandan02/14/06 07:27
±â³×½ººÏ µµÀü, 100¹è »¡¶óÁö°í ½¬¿öÁø ¿µ¾î¿ø¸® ÀÌÈÆ±â11/17/05 09:50
configure mysql php and apache Ashok jaydhar06/08/05 05:53
RE: Parsing XML Variable instead of filesmriti05/17/05 14:09
Parsing XML Variable instead of filematthew bagley04/29/05 12:04
php-mysql to xml to flash mx fatma04/16/05 05:35
website linkAl McNicoll02/07/05 10:00
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¿í12/05/04 10:12
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÇýÁø12/03/04 08:33
½Å.¿ë.ºÒ.·®/Ä«.µå.¿¬.ü/´ë.Ãâ/È¥ÀÚ/ÇØ.°áÇÏ´Â/¹æ.¹ý±èÇö¼­11/30/04 18:58
Ä«/µå/µ¹·Á¸·±â·Î/¸Á°¡Áö½ÅºÐ/²À º¸¼¼¿ä!ÀÌÇýÁø11/25/04 04:24
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæ¿í11/24/04 08:18
½Å.¿ë.ºÒ.·®.ÀÚ/´çÀÏ500/´ë.Ãâ.ºñ.¹ýÀÌÈñÁø11/17/04 06:21
10_ºÐ¾È¿¡ Çö.±Ý40¸¸_¿ø ¸¸.µé,±âÀÌÁ¤¿¬11/15/04 12:41
´ë'Ãâ'°Å'Àý'½Ã'100%µÇ'°Ô'ÇÏ'´Â'¹æ'¹ýÇѰæÇý11/13/04 06:45
RE: class for loading xml doc into arraychris10/01/03 08:44
An MVC framework for PHP using XSLTJustin Grant07/22/03 23:13
RE: xml as results of a formRicardo Garcia03/25/03 18:59
RE: How to convert HTML document to XML document jama03/06/03 04:26
empty xml tagstoshog02/24/03 00:04
RE: XML vs MySQL?pezagent12/25/02 11:08
RE: How to convert HTML document to XML documpezagent12/25/02 10:53
RE: Why XML filespezagent12/25/02 10:47
class for loading xml doc into arrayBrian Sanders12/12/02 15:23
PS. You don't need to set case-folding.../zureash12/11/02 16:41
RE: Case sensitivity/zureash12/11/02 09:54
RE: xml as results of a formHolger11/06/02 09:03
RE: XML vs MySQL?derek10/04/02 00:55
RE: XML vs MySQL?Mattias09/19/02 17:57
NewsBoy vs. newsboy in the example...Michael09/12/02 14:47
RE: What is "format" variable in this exampleRishi08/29/02 22:29
RE: XML vs MySQL?DWard08/28/02 21:09
RE: How about getting just one attributeBinaryGoddess08/28/02 20:01
Newb me could use a few answers...Duane08/28/02 19:17
RE: XML vs MySQL?ednark08/21/02 04:28
RE: What is "format" variable in this exampleStan08/09/02 18:30
RE: php-xml parserSugaya08/09/02 13:42
XML vs MySQL?Andy Christianson08/06/02 14:27
Size of XMl docBhuvnesh Kumar Sharma08/01/02 02:44
RE: What is "format" variable in this exampleTucker McLean07/30/02 21:59
What is "format" variable in this exampleJason Harley07/15/02 10:04
Search any attributes & values in XML Wajid Irfan06/25/02 10:21
php-mysql to xml to flash mxdreadyk06/24/02 04:54
RE: Expat & Sablotron tutorialJim Keohane06/10/02 17:24
Using PHP for form editingsucheta05/17/02 08:37
How to convert HTML document to XML document Hai Nguyen05/13/02 22:48
output controlArman05/07/02 20:18
Why XML filesJames Friesen04/29/02 00:07
RE: Store XML Data in an array to work with itRashmi Balakrishnan04/20/02 08:07
FormsDan03/28/02 18:07
RE: how to connect xml with phpSurya Desaraju03/28/02 13:37
RE: dispay data based on attributes?Surya Desaraju03/28/02 13:35
XML adding, editing, and deleting nodes w/PHPDavid Walker03/27/02 23:24
rdf utf-8 to sql mysqldavid d03/27/02 07:42
RE: how to connect xml with phpavar03/27/02 01:28
RE: How do I insert data in XML into MySQL tablesChris Hilbert03/25/02 17:02
RE: How do I insert data in XML into MySQL tablesRohit03/19/02 16:54
RE: xsl/xml/phpYves03/12/02 17:17
Store XML Data in an array to work with itPeter Autenrieth02/15/02 03:33
RE: Want Associative Array from xml stringibekowies01/28/02 11:45
RE: dispay data based on attributes?Gary Loescher01/17/02 01:48
dispay data based on attributes?ever01/14/02 19:22
dynamic parsinggaucho01/14/02 11:25
php-xml parsersands01/10/02 13:44
RE: How do I insert data in XML into MySQL tablesJawahar Rajamanickam01/07/02 00:50
Want Associative Array from xml stringS. Murali Krishnan01/02/02 06:50
Print, Send link to friend, and Save local?chris allen12/28/01 03:19
RE: How do I insert data in XML into MySQL tablesTobias Beuving12/14/01 15:05
RE: FLASH > PHP > XMLBertram Schaub11/28/01 05:27
xsl/xml/phpFrank11/21/01 06:00
RE: How do I insert data in XML into MySQL tablesRoland11/14/01 05:21
How do I insert data in XML into MySQL tablesRahul11/02/01 23:17
generating xml filesCathy10/30/01 04:05
Selecting Few storiespranay10/18/01 22:45
FLASH > PHP > XMLDirk10/17/01 23:53
RE: Sending XML request via SSL?kim10/14/01 22:12
How about getting just one attributeChad10/04/01 21:08
xml as results of a formMark08/23/01 01:32
RE: Atributes pleaseJames Taylor08/18/01 16:49
RE: XSL / XSLT anyone?Kevin Weir08/14/01 18:55
Expat & Sablotron tutorialjan janssen07/20/01 05:41
Any problems with PHP 4.0.6?rick07/03/01 08:38
RE: Atributes please - Now I've got itLuis Oliveira06/25/01 01:44
Atributes pleaseLuis Guerra Oliveira06/24/01 02:25
Very CoolDan Romanchik06/05/01 09:21
xml_parse generates internal server errorDan Romanchik06/04/01 12:01
Sending XML request via SSL?Dave05/17/01 08:44
Outputing the news as an arrayLuc05/08/01 08:41
NamespacesFang-Pin Chang05/04/01 14:02
RE: xslt and xml and PHP - possible???Urs Gehrig05/04/01 12:15
RE: attributesTommy04/27/01 07:25
xslt and xml and PHP - possible???Ralf Lodde03/04/01 15:50
Case sensitivityLiz03/04/01 03:26
XML is case sensitiveLiz03/04/01 02:59
Correct the examplealain03/03/01 10:10
NOTE to AUTHOR re: XML Justin Grant02/28/01 15:21
passing XML to PHP3Robin Baker02/02/01 14:03
NOTE to AUTHOR re: XML Allen01/31/01 21:47
RE: Problem : WML with PHPAllen01/31/01 21:45
It WORKS!!John Scrivener01/22/01 11:28
Problem : WML with PHPAlbert Tse01/07/01 03:18
RE: WML with PHPDavid Pettersson12/31/00 08:06
attributes? George12/22/00 21:24
RE:attributes ???George12/22/00 08:36
RE: WML with PHPGeorge12/22/00 08:20
WML with PHPMir12/19/00 10:21
RE: attributesMarc Antony Vose12/04/00 02:51
attributesGlen11/22/00 23:07
xml_set_objectJMK10/12/00 14:43
PHP and XMLtom10/11/00 15:01
RE: XSL / XSLT anyone?Alte Rego10/05/00 10:46
XSL / XSLT anyone?Mark Litchfield08/04/00 14:05
XSL / XSLT anyone?Mark Litchfield08/04/00 14:05
Thanks a whole bunch ;-)Tom Henry06/27/00 11:32
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.