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; }
/** * convert array to xml * * @param string $p_arr * @param string $rootName * @return DomDocument * @author Andy Bennett */ public static function convert_array_to_xml($p_arr, $rootName = 'rows') { $doc = new domdocument("1.0", "utf-8"); if (is_array($p_arr) && count($p_arr) != 0) { $arr = array(); if (count($p_arr) > 1) { if ($rootName != "") { $root = $doc->createElement($rootName); } else { $root = $doc->createElement("root"); $rootName = "root"; } $arr = $p_arr; } else { $key = key($p_arr); $val = $p_arr[$key]; if (!is_int($key)) { $root = $doc->createElement($key); $rootName = $key; } else { if ($rootName != "") { $root = $doc->createElement($rootName); } else { $root = $doc->createElement("root"); $rootName = "root"; } } $arr = $p_arr[$key]; } $root = $doc->appendchild($root); self::array_to_xml($doc, $root, $arr, $rootName); return $doc; } }