note 98538 added to function.simplexml-import-dom
| From: | jordantheboss at gmail dot com | Date: | Tue, 22 Jun 2010 00:06:31 +0000 |
| Subject: | note 98538 added to function.simplexml-import-dom | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170359@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 208.69.120.55
Probable Submitter: 97.126.129.157
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-import-dom.php
Edit -- https://master.php.net/note/edit/98538
Del: integrated -- https://master.php.net/note/delete/98538/integrated
Del: useless -- https://master.php.net/note/delete/98538/useless
Del: bad code -- https://master.php.net/note/delete/98538/bad+code
Del: spam -- https://master.php.net/note/delete/98538/spam
Del: non-english -- https://master.php.net/note/delete/98538/non-english
Del: in docs -- https://master.php.net/note/delete/98538/in+docs
Del: other reasons-- https://master.php.net/note/delete/98538
Reject -- https://master.php.net/note/reject/98538
Search -- https://master.php.net/manage/user-notes.php