Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

domdocument.getelementbyid(3) [php man page]

DOMDOCUMENT.GETELEMENTBYID(3)						 1					     DOMDOCUMENT.GETELEMENTBYID(3)

DOMDocument::getElementById - Searches for an element with a certain id

SYNOPSIS
public DOMElement DOMDocument::getElementById (string $elementId) DESCRIPTION
This function is similar to "DOMDocument::getElementsByTagName" but searches for an element with a given id. For this function to work, you will need either to set some ID attributes with "DOMElement::setIdAttribute" or a DTD which defines an attribute to be of type ID. In the later case, you will need to validate your document with "DOMDocument::validate" or DOMDocument::$vali- dateOnParse before using this function. PARAMETERS
o $elementId - The unique id value for an element. RETURN VALUES
Returns the DOMElement or NULL if the element is not found. EXAMPLES
Example #1 DOMDocument::getElementById() Example The following examples use book.xml which contains the following: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE books [ <!ELEMENT books (book+)> <!ELEMENT book (title, author+, xhtml:blurb?)> <!ELEMENT title (#PCDATA)> <!ELEMENT blurb (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ATTLIST books xmlns CDATA #IMPLIED> <!ATTLIST books xmlns:xhtml CDATA #IMPLIED> <!ATTLIST book id ID #IMPLIED> <!ATTLIST author email CDATA #IMPLIED> ]> <?xml-stylesheet type="text/xsl" href="style.xsl"?> <books xmlns="http://books.php/" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <book id="php-basics"> <title>PHP Basics</title> <author email="jim.smith@basics.php">Jim Smith</author> <author email="jane.smith@basics.php">Jane Smith</author> <xhtml:blurb><![CDATA[ <p><em>PHP Basics</em> provides an introduction to PHP.</p> ]]></xhtml:blurb> </book> <book id="php-advanced"> <title>PHP Advanced Programming</title> <author email="jon.doe@advanced.php">Jon Doe</author> </book> </books> <?php $doc = new DomDocument; // We need to validate our document before refering to the id $doc->validateOnParse = true; $doc->Load('book.xml'); echo "The element whose id is 'php-basics' is: " . $doc->getElementById('php-basics')->tagName . " "; ?> The above example will output: The element whose id is 'php-basics' is: book SEE ALSO
DOMDocument::getElementsByTagName. PHP Documentation Group DOMDOCUMENT.GETELEMENTBYID(3)

Check Out this Related Man Page

DOMDOCUMENT.CREATEELEMENT(3)						 1					      DOMDOCUMENT.CREATEELEMENT(3)

DOMDocument::createElement - Create new element node

SYNOPSIS
public DOMElement DOMDocument::createElement (string $name, [string $value]) DESCRIPTION
This function creates a new instance of class DOMElement. This node will not show up in the document unless it is inserted with (e.g.) DOMNode.appendChild(3). PARAMETERS
o $name - The tag name of the element. o $value - The value of the element. By default, an empty element will be created. The value can also be set later with DOMElement::$node- Value. RETURN VALUES
Returns a new instance of class DOMElement or FALSE if an error occurred. ERRORS
/EXCEPTIONS o DOM_INVALID_CHARACTER_ERR - Raised if $name contains an invalid character. EXAMPLES
Example #1 Creating a new element and inserting it as root <?php $dom = new DOMDocument('1.0', 'utf-8'); $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(); ?> The above example will output: <?xml version="1.0" encoding="utf-8"?> <test>This is the root element!</test> NOTES
Note The $value will not be escaped. Use DOMDocument::createTextNode to create a text node with escaping support. SEE ALSO
DOMNode::appendChild, DOMDocument::createAttribute, DOMDocument::createAttributeNS, DOMDocument::createCDATASection, DOMDocument::create- Comment, DOMDocument::createDocumentFragment, DOMDocument::createElementNS, DOMDocument::createEntityReference, DOMDocument::createPro- cessingInstruction, DOMDocument::createTextNode. PHP Documentation Group DOMDOCUMENT.CREATEELEMENT(3)
Man Page