Quickstart: DOM Tree building

Posted on January 15, 2007. Filed under: Quick Start |

1. Document object:

$dom = new DomDocument;

2 . Element:

$docElement = $dom->createElement(’root’) ;

$dom->appendChild($docElement) ;

3. Set attribute:

$docElement->setAttribute(’attrname’, ‘attrvalue’);

4. Element with textual content:

$para = $dom->createElement(’p', ‘Lorem ipsum dolor…’) ;

5. Append child:

$docElement->appendChild($para);

6. Import node:

$import = $dom->importNode($foreignNode, true);

If the second argument is set to true the subtree under $foreignNode will be imported, too. Now append the node named $import (see 5).

Make a Comment

Make A Comment: ( None so far )

blockquote and a tags work here.

  • Bookmarks

Liked it here?
Why not try sites on the blogroll...