note 98538 deleted from function.simplexml-import-dom by danbrown
| From: | danbrown@php.net | Date: | Tue, 22 Jun 2010 00:07:13 +0000 |
| Subject: | note 98538 deleted from function.simplexml-import-dom by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170360@lists.php.net to get a copy of this message | ||
Note Submitter: jordantheboss at gmail dot com
----
In regards to what peter-SPAMrubbish-ATveridon.com mentioned, SimpleXML being able to modify the DOM
is a huge deal. It's way easier to modify XML from the DOM with SimpleXML then it is using the
DOM Functions.
Say you wanted to add a URL to an attribute on a new XML Node; For Example, Using the DOM to Add a
Child node and attribute:
$xmlDoc = DOMDocument::load($xmlFile);
$base = $xmlDoc->getElementsByTagName('base')->item(0);
$path = $xmlDoc->createElement('path');
$attr = $xmlDoc->createAttribute('url');
$attrVal = $xmlDoc->createTextNode($url);
$element[] = $base->appendChild($path);
$element[] = $path->appendChild($attr);
$element[] = $attr->appendChild($attrVal);
Doing so with SimpleXML becomes:
$xmlDoc = DOMDocument::load($xmlFile);
$xmlStr = simplexml_import_dom($xmlDoc);
$newEle = $xmlStr->addChild('path');
$newEle->addAttribute('url',$url);
Also, another important note, if you load the XML file with DOMDocument::load, and plan to transform
it with XSL, your XSL can use relative paths if the XML is loaded in through the DOM, but if you
load it with simplexml_load_file, then try to load the modified SimpleXML object via
DOMDocument::loadXML($xmlStr->asXML()), your XSL relative paths won't work.
Took me a while to figure that one out, so I figured I'd pass on the love.