dom - Get last node in XML using DOMDocument PhP -
i've made class reads value , node name of value , combines array use simple , quick access config in xml. solution works if ill give bottom node, xml looks this.
<?xml version="1.0" encoding="iso-8859-2"?> <settings> <const> <inscript>true</inscript> <title>template</title> </const> <meta> </meta> <db> <user>user</user> <pass>pass</pass> <host>host</host> <name>name</name> </db> <path> <style> <css>/template/view/www/style/</css> <img>/template/view/www/style/img</img> </style> </path> </settings>
now want example whole db node , return array node name wold key , node value, value. im stuck @ this. heres made far.
class config { private static $xml = "lib/config/settings.xml"; private static $xmlroot = "settings"; public static function loadconfig($value) { $domdocument = new domdocument(); $domdocument->load(self::$xml); $settings = $domdocument->getelementsbytagname(self::$xmlroot); try { foreach ($settings $setting) { $configvalue = $setting->getelementsbytagname($value)->item(0)->nodevalue; $confignode = $setting->getelementsbytagname($value)->item(0)->nodename; $test = $setting->getelementsbytagname("path")->item(0)->childnodes->item(2)->nodename; var_dump($test); } $configvalue = explode(' ', trim(preg_replace( '/\s+/', ' ', $configvalue))); $confignode = explode(' ', trim(preg_replace( '/\s+/', ' ', $confignode))); $configarray = array_combine($confignode, $configvalue); return $configarray; } catch(exception $e) { echo '<h1>błąd - '.$e->getmessage().'</h1>'; } }
}
you use xpath this:
$xp = new domxpath($domdocument); $config = array(); foreach ($xp->query('./db/*') $node) { $config[$node->nodename] = $node->textcontent; } return $config;
Comments
Post a Comment