DOM (Document Object Model) in PHP5

DOM (Document Object Model) in PHP5

In php5 DOM is an default extension.
It is used to create the xml/xhtml file in php.You can create the DOM object using the follwoing line.
$doc = new DomDocument();

By using the above object you can easily create the element by using the below code

$newEle = $doc->createElement('elementName'); [elementName - Variable]

After creating the element you can append the element as a child to other element.

$doc->appendChild($newEle);

The above code append the element as a child in the root.If you want to append the element as a child in particular element.
For example:

$dom = new DomDocument();
$ele = $dom->createElement('html');
$chdEle = $dom->createElement('body');
$appEle = $ele->appendChild($chdEle);

$dom->appendChild($ele);

In the above code First the <html> and <body> elements are created,After that the body element is added as a child in html element.

Loading...

+ There are no comments

Add yours