PHP: DOMDocument->createElement() - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument->createElementNS() DOMDocument->createDocumentFragment()

Last updated: Sat, 27 Jan 2007

view this page in

# DOMDocument->createElement()

(no version information, might be only in CVS)

DOMDocument->createElement() -- Create new element node

## Описание

class DOMDocument {
DOMElement createElement ( string name [, string value] )
}

This function creates a new instance of class DOMElement. Этот узел не будет отображаться в документе до тех пор, пока он не будет вставлен, к пример функцией DOMNode->appendChild().

## Список параметров

`name`

  The tag name of the element.

`value`

  The value of the element. By default, an empty element will be created. You can also set the value later with `DOMElement->nodeValue`.

## Возвращаемые значения

Returns a new instance of class DOMElement or `FALSE` if an error occured.

## Errors/Exceptions

`DOM_INVALID_CHARACTER_ERR`

  Raised if `name` contains an invalid character.

## Примеры

Пример 1. Creating a new element and inserting it as root

`<?php
$dom = new DOMDocument('1.0', 'iso-8859-1');
$element = $dom->createElement('test', 'This is the root element!');
// We insert the new element as root (child of the document)
$dom->appendChild($element);
echo $dom->saveXML();
?>`

Результат выполнения данного примера:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <test>This is the root element!</test>

## Смотрите также

DOMNode->appendChild()
DOMDocument->createAttribute()
DOMDocument->createAttributeNS()
DOMDocument->createCDATASection()
DOMDocument->createComment()
DOMDocument->createDocumentFragment()
DOMDocument->createElementNS()
DOMDocument->createEntityReference()
DOMDocument->createProcessingInstruction()
DOMDocument->createTextNode()

add a note User Contributed Notes
DOMDocument->createElement()
mikek dot nospam at nospam dot muonics dot com
03-Mar-2007 05:24
`With regard to the note below about needing htmlentities to avoid warnings about unterminated entity references, I thought it worthwhile to mention that that you don't need to with createTextNode and DOMText::__construct. If you mix both methods of setting text nodes and do (or don't) apply htmlentities consistently to all data to be displayed, you'll get &amp;s (or warnings and badly-formed xml).
It's probably in one's best interest to extend DOMElement and DOMDocument so that it creates a DOMText node and appends it, rather than passing it up to the DOMElement constructor. Otherwise, good luck using (or not using) htmlentities in all the right places in your code, especially as code changes get made.
<?php
class XDOMElement extends DOMElement {
function __construct($name, $value = null, $namespaceURI = null) {
parent::__construct($name, null, $namespaceURI);
}
}
class XDOMDocument extends DOMDocument {
function __construct($version = null, $encoding = null) {
parent::__construct($version, $encoding);
$this->registerNodeClass('DOMElement', 'XDOMElement');
}
function createElement($name, $value = null, $namespaceURI = null) {
$element = new XDOMElement($name, $value, $namespaceURI);
$element = $this->importNode($element);
if (!empty($value)) {
$element->appendChild(new DOMText($value));
}
return $element;
}
}
$doc1 = new XDOMDocument();
$doc1_e1 = $doc1->createElement('foo', 'bar & baz');
$doc1->appendChild($doc1_e1);
echo $doc1->saveXML();
$doc2 = new XDOMDocument();
$doc2_e1 = $doc2->createElement('foo');
$doc2->appendChild($doc2_e1);
$doc2_e1->appendChild($doc2->createTextNode('bar & baz'));
echo $doc2->saveXML();
?>
Text specified in createElement:
<?xml version=""?>
<foo>bar &amp; baz</foo>
Text added via createTextNode:
<?xml version=""?>
<foo>bar &amp; baz</foo>`
sergsokolenko at gmail dot com
30-Jan-2007 04:11
`To avoid warning message "unterminated entity reference" you may use htmlentities() for escaping supplied value:
<?php
//...
$dom->createElement('name', htmlentities($text))
//...
?>`
add a note
DOMDocument->createElementNS() DOMDocument->createDocumentFragment()

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST