class find_by_xpath {
public $head_tag;
public $xpath;
public $cur_xml;
public $id_field;
public $results;
public $parser;
function __construct() {
$this->head_tag = "";
$this->xpath = "";
$this->cur_xml = "";
$this->id_field = "";
$this->results = array();
}
function startHandler($xp, $element, $attribs) {
$element = strtolower($element);
if($element == $this->head_tag) {
$this->cur_xml = "";
}
$this->cur_xml .= "<$element>";
}
function endHandler($xp,$element) {
$element = strtolower($element);
$this->cur_xml .= "$element>";
if ($element == $this->head_tag) {
$xml = simplexml_load_string($this->cur_xml);
$nodes = $xml->xpath($this->xpath);
$count = count($nodes);
if($count > 0) {
$id = $xml->xpath("//".$this->id_field);
$this->results[] = $id[0];
}
$this->cur_xml == "";
}
}
function cDataHandler($xp,$data) {
if(trim($data) != "") {
$data = ereg_replace("&","&",$data);
$data = ereg_replace("<","<",$data);
$data = ereg_replace(">",">",$data);
$this->cur_xml .= $data;
}
}
function find($xpath,$xml_file,$head_tag,$id_field) {
$this->xpath = $xpath;
$this->head_tag = $head_tag;
$this->id_field = $id_field;
$this->parser = xml_parser_create();
xml_set_object($this->parser,$this);
xml_set_element_handler($this->parser,"startHandler","endHandler");
xml_set_character_data_handler($this->parser,"cDataHandler");
$in = fopen($xml_file,"r");
while($line = fgets($in)) {
$line = ereg_replace("&","&",$line);
$good_parse = xml_parse($this->parser,$line,false);
if(!$good_parse) {
echo "there was an error
";
}
}
fclose($in);
return $this->results;
}
}
$form_vars = array_merge($_GET,$_POST);
$xpath = strtolower($form_vars["xpath"]);
$xpath = stripslashes($xpath);
echo $xpath;
$xml_file = $form_vars["xml_file"];
if(!ereg("project_xml_files",$xml_file)) {
$xml_file = "../../project_xml_files/".$xml_file;
}
$head_tag = strtolower($form_vars["head_tag"]);
$id_field = strtolower($form_vars["id_field"]);
$fbx = new find_by_xpath();
$ids = $fbx->find($xpath,$xml_file,$head_tag,$id_field);
echo "XPath: $xpath
";
echo "The following entries contain $xpath:
";
foreach($ids as $id) {
echo "$id_field: $id
";
}
?>