rossh Posted February 28, 2010 Share Posted February 28, 2010 Hi I want to search an xml document, which could get quite large. The search should allow for a keyword search on specified tags and allow the end-user to filter by certain categories. An example xml file would be: <?xml version="1.0" encoding="UTF-8"?> <staff> <member type="management"> <title>Mr</title> <forename>Robert</forename> <surname>Bobby</surname> </member> I've created a class with a method which will return xml data based on specified categories. I'm using XMLReader with expand, dom and simpleXML, but i'm having difficulty adding in an xpath query string for the keywords. I have two questions: [*]Is this the most efficient way of searching a large document? The results from this search with be used in a search page. [*]How would i include an xpath query before i create the simpleXML array? Any help would be really appreciated. <?php class xmlParser{ public $file; public $node; public function __construct($aFile, $aNode){ $this->file = $aFile; $this->node = $aNode; } public function setFile($newFile){ $this->file = $newFile; } public function getFile(){ return $this->file; } public function setPath($newNode){ $this->node = $newNode; } public function getNode(){ return $this->node; } public function queryXML($types){ var_dump($types); $reader = new XMLReader(); $reader->open($this->file); $results = array(); while($reader->read()){ switch($reader->nodeType){ case(XMLREADER::ELEMENT): if($reader->localName == $this->node){ foreach($types as $type){ if($reader->getAttribute('type') == $type){ $node = $reader->expand(); $dom = new DomDocument(); $n = $dom->importNode($node,true); $dom->appendChild($n); $sxe = simplexml_import_dom($n); array_push($results, $sxe); $reader->moveToAttribute('type'); } } } } } return $results; } } $staff_details = new xmlParser('include/staff.xml', 'member'); $staff_details = $staff_details->queryXML(array('management', 'it', 'hr')); var_dump($staff_details) ?> This was my initial attempt, which could be okay for small xml files. This has the query string i need to include in my XMLReader function. <?php class xmlParser{ public $file; public $path; public function __construct($aFile, $aPath){ $this->file = $aFile; $this->path = $aPath; } public function setFile($newFile){ $this->file = $newFile; } public function getFile(){ return $this->file; } public function setPath($newPath){ $this->path = $newPath; } public function getPath(){ return $this->path; } public function countXMLObjects(){ if(file_exists($this->file)) { $data = file_get_contents($this->file); $xml = simplexml_load_string($data); }else{ exit('Sorry there seems to be a problem, please contact the administrator'); } return $count = count($xml->xpath("$this->path")); } public function queryXML($start=null, $limit=null){ if(file_exists($this->file)) { $data = file_get_contents($this->file); $xml = simplexml_load_string($data); }else{ exit('Sorry there seems to be a problem, please contact the administrator'); } $data = $xml->xpath($this->path); if(isset($start) && isset($limit)){ $data = array_slice($data, $start, $limit); //Used for paging } return $data; } } ?> xpath query i want to use: $query = "//staff/member[contains(translate(firstname,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'$keywords')] Quote Link to comment https://forums.phpfreaks.com/topic/193666-searching-xml-using-xmlreader-dom-simplexml-and-xpath/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.