Learning: Migrating from the eZXML library to PHP 5 DOM

API replacements

DOM Document

eZXML

DOMDocument

Remarks

$xml = new eZXML()
$dom = $xml->domTree( $xmlDoc )

$dom = new DOMDocument()
$success = $dom->loadXML( $xmlDoc )

there were some extra options which we need to check, like character set conversion

eZDOMDocument

DOMDocument

Remarks

->create_element( $name [, $attributes] )

$element = $dom->createElement( $name )
$element->setAttribute( $attrName, $attrValue )

->createElementNode( $name [, $attributes] )

$element = $dom->createElement( $name )
$element->setAttribute( $attrName, $attrValue )
...

->createElementTextNode( $name, $text [, $attributes] )

$element = $dom->createElement( $name )
$element->appendChild( $dom->createTextNode( $text ) )
$element->setAttribute( $attrName, $attrValue )
...

 

->dump_mem()

->saveXML()

 

->elementsByNameNS( $name, $namespaceURI )

->getElementsByTagNameNS( $namespaceURI, $name )

can be called on each DOMNode, not only on a DOMDocument

->get_root()

->documentElement

property

->Root

->documentElement

property

->root()

->documentElement

property

->setRoot( $element )

->appendChild( $element )

 

->toString()

->saveXML()

 

DOM Node

eZDOMNode

DOMNode and subclasses

Remarks

->append_child( $node )

->appendChild( $node )

 

->attributeCount()

->attributes->length

see below

->attributes()

->attributes

property, returns DOMNamedNodeMap instead of array now, use $attributes->length property instead of count( $attributes ), see above

->attributeValue( $name )

->getAttribute( $name )

 

->attributeValueNS( $name, $namespaceURI )

->getAttributeNS( $namespaceURI, $name )

pay attention to the changed order of the function arguments

->Children

->childNodes

property, returns DOMNodeList instead of array now, use $children->length property instead of count( $children )

->children()

->childNodes

see above

->childrenCount()

->childNodes->length

 

->content()

->textContent

property

->dump_mem()

$element->ownerDocument->saveXML( $element )

 

$element->dump_mem()

$element->ownerDocument->saveXML( $element)

 

->elementAttributeValueByName( $name, $attributeName )

$firstElement = $element->getElementsByTagName( $name )->item( 0 )
$attributeValue = $firstElement->getAttribute( $attributeName )

 

->elementByName( $name )

->getElementsByTagName( $name )->item( 0 )

 

->elementChildrenByName( $name )

$firstElement = $element->getElementsByTagName( $name )->item( 0 )
$children = $firstElement->childNodes

 

->first_child()

->firstChild

property

->firstChild()

->firstChild

property

->firstElementByName( $name )

->getElementsByTagName( $name )->item( 0 )

see elementByName

->get_attribute( $name )

->getAttribute( $name )

 

->get_elements_by_tagname( $name )

->getElementsByTagName( $name )

Returns all descendant elements with the name $name, not only direct children. To get only direct children with the name $name, use DOMXPath or loop over ->childNodes

->hasChildren()

->hasChildNodes()

 

->lastChild()

->lastChild

property

->localName()

->localName

property

->name()

->localName

property

->namespaceURI()

->namespaceURI

property

->nextSibling()

->nextSibling

property

->node_value()

->nodeValue

property

->parentNode()

->parentNode

property

->prefix(), ->setPrefix( $prefix )

->prefix

writable property

->previousSibling()

->previousSibling

property

->remove_attribute( $name )

->removeAttribute( $name )

 

->set_attribute( $name, $value )

->setAttribute( $name, $value )

 

->textContent()

->textContent

property

$element->toString()

$element->ownerDocument->saveXML( $element)

 

->type()

->nodeType

property, one of the predefined XML_xxx_NODE constants

Important notes

The DOMElement::insertBefore function returns the appended node, and you should use it! Otherwise you can get unexpected results or uncaught exceptions (like DOMException with message "Not Found Error").

When looping over the DOMElement::childNodes property, you can not remove or move any of the existing children, or insert new one. This will cause the property to change and your loop will apparently skip certain children or handle certain children twice. Instead of looping directly over childNodes, you can first make an array containing the children and loop over that array to make your modifications. In case you only want to remove certain items, looping over childNodes in reverse order (from the last one to the first one) is a solution as well.

The same applies to the DOMElement::attributes property.