note 43471 added to function.xpath-new-context

From: Date: Tue, 22 Jun 2004 14:52:41 +0000
Subject: note 43471 added to function.xpath-new-context
Groups: php.notes 
Request: Send a blank email to php-notes+get-71502@lists.php.net to get a copy of this message
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>"; ---- Manual Page -- http://www.php.net/manual/en/function.xpath-new-context.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+43471 Delete -- http://master.php.net/manage/user-notes.php?action=delete+43471&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+43471&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#71502) next »