Articles Php Functions
DOM XML: An Alternative to Expat - Page 6
by: Matt Dunford
December 27, 2000
Another example (adding data)
Since the xml is loaded into memory as a tree, we can easily
manipulate the data. We can add branches or nodes when necessary.
Say we want to add an employee to our xml file.
# quick function for making child nodes
# adds a new child node to parent node
# return the newly added child as a reference
# load xml file and get root node
$doc = xmldocfile("employees.xml") or die("Do you even have any employees?");
$root = $doc->root();
# give the new employee a name
$newguy = make_node($root,"employee","");
# add the new guy's name
# add his position
$position = make_node($newguy,"position","Backup Gnome");
# set the 'type' attribute
# dump our altered xml doc to the browser
This will print the xml to the browser, so you will most likely have
to 'View the Source' in order to see the data.
That's pretty much all there is to DOM xml. It's a simple approach to
parsing and manipulating xml in your scripts. I hope this article
will shed more light in this dusty corner of php.
php domxml source