#native_company# #native_desc#
#native_cta#

DOM XML: An Alternative to Expat Page 6

By Matt Dunford
on 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.

<?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.

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.
— Matt

References

DOM reference
http://www.w3.org/TR/
libxml, an essential library for building dom
ftp://ftp.gnome.org/pub/GNOME/stable/sources/libxml/
php domxml source
php-4.0.2/ext/domxml.

1
|
2
|
3
|
4
|
5
|
6