#native_company# #native_desc#
#native_cta#

DOM XML: An Alternative to Expat Page 2

By Matt Dunford
on December 27, 2000

The Objects Used In DOM

At this point, you are probably wondering what is a DomNode. This is
a good place to start talking about the objects that are included in
the module. There are five objects defined by DOM: DomDocument,
DomNode, DomAttribute, DomDtd, and DomNamespace. We are going to be
focusing primarily on the DomDocument and DomNode objects because they
are the most useful.

The Node object

Here is an overview of what the DomNode object contains.
class DomNode
	properties:
		name
		content
		type
	methods:
		lastchild() 
		children() 
		parent() 
		new_child( $name,$content ) 
		getattr( $name ) 
		setattr( $name,$value ) 
		attributes() 
The properties need some elaboration.
  • The name property is the actual tag name of the node. A node which
    refers to the the title tags would have the name of ‘title’.
  • The content property is usually empty. However, text nodes use this
    property to hold text.
  • The type property is a constant which defines exactly what kind of
    object the node is. There can be several types of DomNode objects. A
    list of constants are online at
    http://www.php.net/manual/ref.domxml.php. For example, a DomNode
    containing text would have a type of XML_TEXT_NODE.
The methods need to be explained, as well.
  • lastchild() returns the last entry from a node’s children.
  • parent() returns a node’s parent.
    For instance, the parent of our title node would be ‘book’.
  • children() returns an array of a node’s child nodes. For example, the
    children of node author would be ‘name’ and ‘birthdate’.
  • new_child() takes a name and some content as arguments and adds a
    new DomNode to its children.
  • getattr() and setattr() both deal with attributes. One
    fetches the value, the other sets it.
  • attributes() returns an array of DomAttribute objects.
The DomDocument object
The DomDocument object is also important.
class DomDocument
	properties:
		version 
		encoding 
		standalone
		type
	methods:
		root() 
		children() 
		add_root( $node ) 
		dtd() 
		dumpmem() 
The properties are pretty self explanatory.
  • ‘version’ refers to the xml version of the document.
  • ‘encoding’ refers to the text encoding.
  • ‘standalone’ is a boolean value determining whether the document is
    standalone or not.
  • The ‘type’ property has already been explained. A Document object will
    most likely have the type of XML_DOCUMENT_NODE.
The methods are pretty simple too.
  • root() returns the root node of a document. If we loaded our sample
    xml file as a DomDocument object, the root node would refer to ‘book’.
  • children() works just as it did in DomNode.
  • add_root() adds a new root node to the xml document. You would use
    this if you wanted to supplant the ‘book’ node with another node.
  • dtd() returns the xml document’s dtd.
  • dumpmem() returns a string representation of the xml data.