Voting

: six minus four?
(Example: nine)

The Note You're Voting On

estill at gvtc dot com
18 years ago
Note that the second parameter (value), although convenient, is non-standard. You should create elements like this instead:

<?php
$doc
= new DOMDocument('1.0', 'iso-8859-1');

$root = $doc->createElement('test');
$doc->appendChild($root);

$root_text = $doc->createTextNode('This is the root element!');
$root->appendChild($root_text);

print
$doc->saveXML();
?>

Or, alternatively, extend the DOMDocument class and add your own custom, convenience method to avoid intruding on the standard:

<?php
class CustomDOMDocument extends DOMDocument {
function
createElementWithText($name, $child_text) {
// Creates an element with a child text node

// @param string $name element tag name
// @param string $child_text child node text

// @return object new element

$element = $this->createElement($name);

$element_text = $this->createTextNode($child_text);
$element->appendChild($element_text);

return
$element;
}
}

$doc = new CustomDOMDocument('1.0', 'iso-8859-1');

$root = $doc->createElementWithText('test', 'This is the root element!');
$doc->appendChild($root);

print
$doc->saveXML();
?>

Also use caution with (or avoid) the 'DOMElement->nodeValue' property. It can return some unexpected values and changing its value will replace (remove) all descendants of the element with a single text node. It's also non-standard; according to the DOM spec it should return NULL.

<< Back to user notes page

To Top