PHP DOM

On Centos 5 the PHP DOM extension is included in the PHP XML package !

# yum install php-xml
# service httpd restart


Being able to use PHP DOM to read/write and modify XML files can be useful when accessing REST web services.

DOM Document

$doc = new DOMDocument();

$doc->loadXML($xml);
$doc->formatOutput=true;


DOM read value

$nodes = $doc->getElementsByTagName('books');
foreach($nodes as $n) {
	echo $n->nodeName.$n->nodeValue;
}


DOM modify value

$n->nodeValue=$newvalue;


DOM read attribute

$a = $n->attributes->getNamedItem('id');
$a = $n->attributes->getNamedItem('uid');


DOM modify attribute

$oodes = $n->getElementsByTagName('book');
foreach($oodes as $o) {
	$o->setAttribute("id", 2);
	$o->setAttribute("uid", 34);
}


DOM Save XML

$doc->saveXML()



REFERRERS
PhpXml
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki