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.
manipulate the data. We can add branches or nodes when necessary.
Say we want to add an employee to our xml file.
<?php
# quick function for making child nodes
function make_node($parent,$name,$content)
{
# adds a new child node to parent node
$parent->new_child($name,$content);
# return the newly added child as a reference
return $parent->lastchild();
}
# 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
make_node($newguy,"name","New Guy");
# add his position
$position = make_node($newguy,"position","Backup Gnome");
# set the 'type' attribute
$position->setattr("type","intern");
# dump our altered xml doc to the browser
echo $doc->dumpmem();
?>
This will print the xml to the browser, so you will most likely have
to ‘View the Source’ in order to see the data.
to ‘View the Source’ in order to see the data.
Conclusion
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.
parsing and manipulating xml in your scripts. I hope this article
will shed more light in this dusty corner of php.
— Matt
References
domxml functions for php
http://www.php.net/manual/ref.domxml.php
http://www.php.net/manual/ref.domxml.php
DOM reference
http://www.w3.org/TR/
http://www.w3.org/TR/
libxml, an essential library for building dom
ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxml/
ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxml/
Short intro on the difference between DOM and SAX
http://www.builder.com/Programming/XMLToday/ss01.html?tag=st.bl.7267.dir1.XMLToday_01
http://www.builder.com/Programming/XMLToday/ss01.html?tag=st.bl.7267.dir1.XMLToday_01
php domxml source
php-4.0.2/ext/domxml.
php-4.0.2/ext/domxml.