Esempio n. 1
0
 public static function xml2Array($arr, $func = "")
 {
     if ($arr instanceof SimpleXMLElement) {
         return XmlUtil::xml2Array((array) $arr, $func);
     }
     if ($arr instanceof DOMElement || $arr instanceof DOMDocument) {
         return XmlUtil::xml2Array((array) simplexml_import_dom($arr), $func);
     }
     $newArr = array();
     if (!empty($arr)) {
         foreach ($arr as $key => $value) {
             $newArr[$key] = is_array($value) || $value instanceof DOMElement || $value instanceof DOMDocument || $value instanceof SimpleXMLElement ? XmlUtil::xml2Array($value, $func) : (!empty($func) ? $func($value) : $value);
         }
     }
     return $newArr;
 }
Esempio n. 2
0
<?php

require "vendor/autoload.php";
$xml = \ByJG\Util\XmlUtil::createXmlDocumentFromStr('<root />');
$myNode = \ByJG\Util\XmlUtil::createChild($xml->documentElement, 'mynode');
\ByJG\Util\XmlUtil::createChild($myNode, 'subnode', 'text');
\ByJG\Util\XmlUtil::createChild($myNode, 'subnode', 'more text');
$otherNode = \ByJG\Util\XmlUtil::createChild($myNode, 'othersubnode', 'other text');
\ByJG\Util\XmlUtil::addAttribute($otherNode, 'attr', 'value');
echo $xml->saveXML();
print_r(\ByJG\Util\XmlUtil::xml2Array($xml));
$node = \ByJG\Util\XmlUtil::selectSingleNode($xml, '//subnode');
echo $node->nodeValue . "\n";
$node = \ByJG\Util\XmlUtil::selectSingleNode($myNode, '//subnode');
echo $node->nodeValue . "\n";
$nodeList = \ByJG\Util\XmlUtil::selectNodes($xml, '//subnode');
foreach ($nodeList as $node) {
    echo $node->nodeName;
}
echo "\n";
$nodeList = \ByJG\Util\XmlUtil::selectNodes($myNode, '//subnode');
foreach ($nodeList as $node) {
    echo $node->nodeName;
}
echo "\n";
\ByJG\Util\XmlUtil::addNamespaceToDocument($xml, 'my', 'http://www.example.com/mytest/');
echo $xml->saveXML() . "\n";
\ByJG\Util\XmlUtil::createChild($xml->documentElement, 'nodens', 'teste', 'http://www.example.com/mytest/');
\ByJG\Util\XmlUtil::createChild($xml->documentElement, 'my:othernodens', 'teste');
echo $xml->saveXML() . "\n";
$nodeList = \ByJG\Util\XmlUtil::selectNodes($xml, '//my:othernodens', ['my' => 'http://www.example.com/mytest/']);