function nodeSet($id = null) { if ($id and is_array($id)) { return $id[0]; } else { $dom = new domdocument(); $dom->loadXML("<root>this is from an external DomDocument</root>"); return $dom->documentElement; } }
<?php $dom = new domdocument(); $data = file_get_contents(dirname(__FILE__) . "/xinclude.xml"); $reldir = str_replace(getcwd(), ".", dirname(__FILE__)); if (DIRECTORY_SEPARATOR == '\\') { $reldir = str_replace('\\', "/", $reldir); } $data = str_replace('compress.zlib://ext/dom/tests/', 'compress.zlib://' . $reldir . "/", $data); $dom->loadXML($data); $dom->xinclude(); print $dom->saveXML() . "\n"; foreach ($dom->documentElement->childNodes as $node) { print $node->nodeName . "\n"; }
public static function splitToNodeset($strList, $separator = ",") { $dom = new domdocument(); $dom->loadXML("<list />"); $docEl = $dom->documentElement; $arr = explode($separator, $strList); $found = false; foreach ($arr as $item) { if (!empty($item)) { $found = true; $element = $dom->createElement("item", $item); $element->setAttribute("value", $item); $docEl->appendChild($element); } } return $dom->documentElement; }
<?php $xml = <<<XML <foo> \t<bar>foobar</bar> </foo> XML; $d = new domdocument(); $d->dynamicProperty = new stdclass(); $d->loadXML($xml); print_r($d);