{"id":53,"date":"2005-08-05T15:32:38","date_gmt":"2005-08-05T20:32:38","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code2\/category\/php\/53"},"modified":"2008-01-21T21:34:33","modified_gmt":"2008-01-22T02:34:33","slug":"basic-xml-parsing-example","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/PHP\/53\/","title":{"rendered":"Basic XML Parsing Example"},"content":{"rendered":"<p>This is a basic example of an event-based XML parser in PHP.  I got it from <a href=\"http:\/\/us2.php.net\/xml\" title=\"PHP xml documentation\">php.net<\/a>. The example was written by Mauricio Massaia. I modified it slightly to take a string or a file address. I also added lines to print the results.<\/p>\n<p><!-- technorati tags start --><\/p>\n<p style=\"text-align:right;font-size:10px;\">Technorati Tags: <a href=\"http:\/\/www.technorati.com\/tag\/networked objects\" rel=\"tag\">networked objects<\/a>, <a href=\"http:\/\/www.technorati.com\/tag\/networks\" rel=\"tag\">networks<\/a><\/p>\n<p><!-- technorati tags end --><br \/>\n<!--more--><\/p>\n<p>Instantiating the class with <tt>$xml = new xmlClass()<\/tt> sets up the XML parsing engine and defines handlers that will get called automatically whenever a new XML tag is encountered. You can parse either a file by using <tt>$xml->parse(\"filename.xml\")<\/tt> or a string using <tt>$xml->parseString($someString)<\/tt>.  When a tag that begins an XML token is encountered, <tt>startHandler()<\/tt> will get called automatically. Add any code there to keep track of the token&#8217;s name or attributes.  Likewise, <tt>endHandler()<\/tt> gets when a closing tag is encountered.  <tt>dataHandler()<\/tt> returns all the text inside the tags.<\/p>\n<pre>\n<?\n\/*xmlClass\nexample:\n\ninclude(\"xmlClass.php\");\n\n$xml = new xmlClass();\n$xml->parse(\"directory\/filename.xml\");\n$xml->parseString($someString);\n*\/\n\nclass xmlClass{\n\n   \/\/xml\n   var $xml;\n   var $path;   \n   var $tagName;\n   var $xmlstring;\n  \n   \/\/counter\n   var $index = 0;\n  \n   function xmlClass(){    \n       $this->xml = xml_parser_create();\n       xml_set_object($this->xml,$this);\n       xml_set_character_data_handler($this->xml, 'dataHandler');   \n       xml_set_element_handler($this->xml, \"startHandler\", \"endHandler\");\n  \n   }\n  \n  \n   function parse($path){\n      \n       $this->path = $path;\n      \n       if (!($fp = fopen($this->path, \"r\"))) {\n           die(\"Cannot open XML data file: $file\");\n           return false;\n       }\n      \n       while ($data = fread($fp, 4096)) {\n           if (!xml_parse($this->xml, $data, feof($fp))) {\n               die(sprintf(\"XML error: %s at line %d\",\n                           xml_error_string(xml_get_error_code($this->xml)),\n                           xml_get_current_line_number($this->xml)));\n               xml_parser_free($this->xml);\n           }\n       }\n      \n       return true;\n   }\n   \nfunction parseString($string){     \n       $this->xmlstring = $string;\n      $data =  $this->xmlstring;\n      \n             if (!xml_parse($this->xml, $data)) {\n               die(sprintf(\"XML error: %s at line %d\",\n                           xml_error_string(xml_get_error_code($this->xml)),\n                           xml_get_current_line_number($this->xml)));\n               xml_parser_free($this->xml);\n           }\n       return true;\n   }\n\n   function startHandler($parser, $name, $attribs){\n       \/\/add your commands here\n       echo \"<p>$name<br>\";\n       echo \"$attribs<br>\";\n   }\n\n   function dataHandler($parser, $data){\n       \/\/add your commands here\n       echo \"$data<br>\";\n   }\n\n   function endHandler($parser, $name){\n           echo \"<\/p>\";\n       $this->tagName = \"\";\n   }\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I also added lines to print the results.Instantiating the class with $xml = new xmlClass() sets up the XML parsing engine and defines handlers that will get called automatically whenever a new XML tag is encountered.  You can parse either a file by using $xml->parse(&#8220;filename.xml&#8221;) or a string using $xml->parseString($someString).<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-53","post","type-post","status-publish","format-standard","hentry","category-PHP"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":0,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}