note 43471 deleted from function.xpath-new-context by sobak

From: Date: Tue, 22 Nov 2016 03:41:54 +0000
Subject: note 43471 deleted from function.xpath-new-context by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207415@lists.php.net to get a copy of this message
Note Submitter: augusto at omnitec dot it ---- Split easily XPath result into associative array: function xpath_parser($xml_file, $xpath_query) { $xml_file = realpath($xml_file); if (file_exists($xml_file)) { $doc = domxml_open_file($xml_file); $ctx = $doc->xpath_new_context(); if ($xpathObj = @$ctx->xpath_eval($xpath_query)) { $return = array(); foreach($xpathObj->nodeset as $risultato) { $tagName = $risultato->tagname(); $content = $risultato->get_content(); if (array_key_exists($tagName, $return)) { $return[$tagName][] = $content; }else { if (count($doc->get_elements_by_tagname($tagName)) > 1) { $return[$tagName] = array($content); }else { $return[$tagName] = $content; } } } }else { return("<b>Error :</b> Wrong XPath query"); } }else { return("<b>Errore :</b> File not exist or wrong filepath!"); } return $return; } usage: $xml_file = "./listaCD.xml"; $query = "/listacd/*"; echo "<pre>"; print_r(xpath_parser($xml_file, $query)); echo "</pre>";

« previous php.notes (#207415) next »