Sr. Developer, Backend
Professional Technical Resources
US-OR-Eugene

Justtechjobs.com Post A Job | Post A Resume

DOM XML: An Alternative to Expat
Overview: An alternative to expat.
There are many xml tutorials for php on the web, but few show how to parse xml using DOM. I would like to take this opportunity to show there is an alternative to the widespread SAX implementation for php programmers.
DOM (Document Object Model) and SAX (Simple API for XML) have different philosophies on how to parse xml. The SAX engine is extremely event-driven. When it comes across a tag, it calls an appropriate function to handle it. This makes SAX very fast and efficient. However, it feels like you're trapped inside an eternal loop when writing code. You find yourself using many global variables and conditional statements.
On the other hand, the DOM method is somewhat memory intensive. It loads an entire xml document into memory as a hierarchy. The upside is that all of the data is available to the programmer organized much like a family tree. This approach is more intuitive, easier to use, and affords better readability.
In order to use the DOM functions, you must configure php by specifying the '--with-dom' argument. They are not a part of the standard configuration. Here is a sample compilation.
%> ./configure --with-dom --with-apache=../apache_1.3.12
%> make
%> make install
How DOM structures XML
Since DOM loads an entire xml string or file into memory as a tree, this allows us to manipulate the data as a whole. To show what xml looks like as a tree, take this xml document as an example.
<?xml version="1.0"?>

<book type="paperback">
	<title>Red Nails</title>
	<price>$12.99</price>
	<author>
		<name first="Robert" middle="E" last="Howard"/>
		<birthdate>9/21/1977</birthdate>
	</author>
</book>
The data would be structured like this.
DomNode book
	|
	|-->DomNode title
	|		|
	|		|-->DomNode text
	|
	|-->DomNode price
	|		|
	|		|-->DomNode text
	|
	|-->DomNode author
			|
			|-->DomNode name
			|
			|-->DomNode birthdate
					|
					|-->DomNode text
Any text enclosed within tags are really nodes in themselves. For instance, "Red Nails" is a child node of title, "$12.99" is a child node of price.
[ Next Page ]


Comments:
PHP5 DOM TEMPLATE ENGINESimon Jackson10/03/08 02:36
how to install libxml2Anjali01/07/05 04:52
succesfully Enabling DOM using phpTriad W32Arturo Cobos05/18/03 18:38
RE: intalling sablotron YinD02/13/03 04:40
RE: DOM with IISskynet01/06/03 18:36
intalling sablotron mounir10/08/02 05:54
RE: Error calling DOMXML function under WindoMads Østerby10/07/02 04:37
Error calling DOMXML function under Windowslian09/16/02 18:20
accessing second XML file from in xslAmrit Lalli08/23/02 16:15
DOM with IISDavid08/03/02 13:02
loading in a string vs. fileDenise07/22/02 14:08
RE: Adding xml-stylesheet tag? Ross Golder07/11/02 17:35
appending and saveallen coombs07/05/02 09:51
Add a multiLine and special characters nodeTitouille07/02/02 11:05
RE: Failed to load external entity.Paulo06/25/02 13:23
XML DOM helpBen06/14/02 08:50
help sos!dysow06/09/02 23:00
RE: Installing DOMXML under WindowsGeorge Yun06/02/02 23:12
Installing DOMXML under Windows with iis5why05/30/02 22:39
installing DOMXML problemGeorge Yun05/30/02 20:54
domxml_xslt_process problem !?!?Claudiu Balanean05/27/02 09:37
RE: Problem with node->set_contentRob Richards05/18/02 10:04
RE: Deleting a forumsam05/17/02 00:23
Problem with node->set_contentMirax05/16/02 08:20
Correction of example on page 4 for PHP 4.2.0Vlado S.05/13/02 10:20
XML DOMmichel05/05/02 11:14
XML DOMmichel05/05/02 11:13
Sample to change or add dataChristiaan Schaake04/29/02 10:16
RE: Error loading DOM XMLYogesh Randhawa04/26/02 02:38
RE: Error loading DOM XMLDjango Janny04/25/02 04:14
Failed to load external entity.Deke04/25/02 04:11
tutorial does not work?Christian04/24/02 08:19
Adding xml-stylesheet tag? Robert Strand04/22/02 05:30
RE: <![CDATA[ A ]]> : Urgent help neededemthanh04/18/02 12:38
RE: Installing DOMXML under WindowsMads Østerby04/15/02 12:51
And about performances ?ThM04/11/02 16:51
no nmake... can't install libxml... help?Rudolf Vavruch04/03/02 10:27
RE: DOM/XML & PHP 4.1.XBes03/28/02 16:54
RE: DOM/XML & PHP 4.1.XGal03/25/02 08:51
RE: DOM/XML & PHP 4.1.XGal03/24/02 07:27
RE: Installing DOMXML under WindowsGeorge02/26/02 14:15
CDATA How ?jack3483@ifrance.Com02/14/02 09:02
RE: Finding libxml-2.2.7Kalpix02/08/02 02:11
Overhead of node deletionssiva02/06/02 22:24
DOM/XML & PHP 4.1.XJoe Thielen02/04/02 22:15
RE: Error loading DOM XMLMac Martine01/30/02 01:05
RE: Error loading DOM XMLMac Martine01/30/02 01:04
PHP & IIS, Win2kzman01/29/02 15:15
children of child node??Sandeep Murthy01/23/02 05:39
Differences with PHP 4.1.1 ?Philippe Langlois01/21/02 02:20
RE: insert node from one object into another?Dietmar Glachs12/20/01 03:49
RE: Installing DOMXML under WindowsMads Østerby12/19/01 10:29
want Example add,delete,insert and updateThai Girl12/17/01 05:24
RE: Installing DOMXML under WindowsHaris12/14/01 03:15
<![CDATA[ A ]]> : Urgent help neededRahul12/08/01 09:02
RE: DOM does work with php4.0.6 in WindowsThomas Wasserberg12/04/01 06:15
XSL transformation supportRichard Borrie11/28/01 07:47
RE: Installing DOMXML under WindowsMads Østerby11/25/01 14:37
RE: How to put xml data into database?Antonio Carvalho11/20/01 05:00
RE: insert node from one object into another?Arne Blankerts11/14/01 06:51
Problem with PHP 4.0.6 under WindowsHans Hagmann11/06/01 02:26
DOM does work with php4.0.6 in WindowsCathy10/30/01 06:36
RE: DOM does not work anymore in newer PHP'sCathy10/30/01 04:53
DOM does not work anymore in newer PHP'sMelvin10/26/01 04:56
Finding libxml-2.2.7Cathy10/19/01 07:40
RE: Installing DOMXML under WindowsMax Zilla10/14/01 05:31
RE: Installing DOMXML under Windowsmaxzilla10/04/01 09:56
wondelful for DOM referencemuruganselvaraj09/27/01 04:17
returning XPath resultsDaniel Allen09/01/01 04:58
RE: ![CDATA[ ]]Jean-Charles Preaux08/29/01 04:31
RE: Installing DOMXML on Windows!igor stuyver08/15/01 09:36
RE: Installing DOMXML under WindowsTomcio08/01/01 20:55
Error loading DOM XMLTom08/01/01 19:06
RE: Deleting a nodeChristophe RISCH07/30/01 07:20
Very Good Tutorialjan janssen07/20/01 05:38
RE: Deleting a nodeThomas Schädler05/27/01 03:18
![CDATA[ ]]Olivier Le Roy05/25/01 06:49
DOMXML an NamespacesBerhard Zwischenbrugger 05/07/01 09:05
PHP 4.0.5 and DOMSylvain B05/07/01 02:43
RE: xpath_eval functionDietmar04/20/01 06:46
xpath_eval functionVincenzo04/13/01 08:30
Adding a new nodeDominique Paquin03/30/01 15:00
RE: Deleting a nodeDietmar03/27/01 07:16
RE: Deleting a nodethor03/21/01 09:38
RE: Just one problem - Character supportBobbisson03/13/01 12:38
RE: insert node from one object into another?Dietmar Glachs03/09/01 07:21
RE: Set content / delete a nodeDominique Paquin03/08/01 10:07
insert node from one object into another?Hauke Haastert03/07/01 09:59
RE: Set content / delete a nodejagan kommineni03/06/01 20:17
InformativeMohanraj02/28/01 21:43
Installing DOMXML on Windows!Mads Østerby02/24/01 10:36
Installing DOMXML under WindowsMads Østerby02/24/01 10:30
RE: How to put xml data into database?Stuart Begg02/20/01 20:48
Set content / delete a nodeStuart Begg02/20/01 20:11
RE: Deleting a nodeDietmar Glachs02/12/01 00:45
RE: Deleting a nodeJody Nickel02/08/01 20:24
XPathDaniel Fairs02/05/01 04:23
How to put xml data into database?kevin zhang02/01/01 21:57
Just one problem - Character supportLuis Oliveira01/31/01 13:28
commentsR. Yuvaraju01/29/01 10:50
RE: new DOM features im 4.0.4peter sabaini01/29/01 06:55
textnodes into existing nodeRobin Gruyters01/24/01 08:59
Great ArticleNavid Yar01/23/01 05:49
insert a node above another one.n000b01/17/01 23:02
Deleting a nodeJens Wabnitz01/15/01 12:35
RE: Changing node's contentJohn Plato01/15/01 04:16
Changing node's contentSergey Kanareykin01/09/01 17:24
Page Display ProblemTac01/08/01 09:53
Choice of Method & Property Namesterry chay01/04/01 19:14
PHP 4.0.4, DOMXML, & Memoryterry chay01/04/01 18:59
new DOM features im 4.0.4Luis Argerich01/02/01 06:40
Very coolLuis Oliveira01/01/01 17:28
RE: Portability issueRalf Weber12/29/00 09:56
Portability issuePaul Meagher12/28/00 11:50
Some Problems I see/haveOliver Kurz12/28/00 10:14
very interestingKevin Monroe12/28/00 08:18
 

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.