PHP  XML parser

using PHP - you can read, write, manipulate an xml document,but you will need an xml parser.

the php provides two type of parser:

  • dom parser : this parser transforms an xml document into a tree structure. using it we can   easily work with xml nodes (element, attributes).
  • event-based parser : this focus on the content of the xml documents, not their structure.   the event-based parser views an xml documents as a series of events. when an event occurs    it call a event handler to handle it.

how create a XML parser

the php xml_parser_create() function is used to create a new parser resource.

syntax:

         xml_parser_create( encoding ) 
  • encoding : optional. specifies the character encoding for the input/output.

how xml parser works with PHP

  • first the xml_parser_create() a new parser resource.
  • create two event handler function to handle the start and end of an xml element
  • register these function with the parser using xml_set_element_handler() function
  • create a event handler function to handle any character data that may be found inside tag
  • register this function with the parser using xml_set_character_data_handler()
  • parse the xml document by calling the xml_parse() function
  • if parser no longer needed, destroy it by calling xml_parser_free()

PHP XML Paser Example

the event-parser use this stocklist.xml file in below example:

     <?xml    version="1.0"    encoding= "utf-8" ?>
     
        <stocklist>
           
               <item  type = "fruit">
               
                    <name>apple</name>
                    
                      <price>12.12</price>
                        
                         <quantity>220</quantity>
                         
               </item>
               
        </stocklist>

example : event-parser php script file 'xml_parser.php'

xml code

           <name>apple</name>

if an event-based parser meet with this code in xml document trigger a series of three events:

  • first event trigger when event-parser meet start element <name>
  • second event trigger when event-parser meet element data - apple
  • third event trigger when event-parser meet close element </name>
<html    xmlns = "http://www.w3.org/1999/xhtml"   xml:lang="en" lang="en">

  <head> <title>parsing an xml file</title></head>
  
    <body>
    
         <h1>parsing an xml file</xml>
         
          <pre>
            
                 <?php
                 
               function   startelementhandler( $parser,  $element, $attribute ){
                       
                            // start element handler
                            
                    echo  "start tag : $element";
                              
                    if( $attribute ) echo "attributes :- ";
                              
                    foreach ( $attribute as $item => $value) echo "$item = $value";
                              
                              echo  "<br/>";
                              
                          }
                          
               function   endelementhandler( $parser,  $element ){   
                                
                       // end element handler
                              
                       echo  "end of element : ". $element;
                              
                       echo  "<br/>";
                         
                           }
                           
               function    characterhandler( $parser,  $data ){    
                              
                              // data handler 
                              
                              echo  "character data:" . htmlspecialchars ( $data );
                              
                              echo  "<br/> ";
                              
                          }
                          
               function   parseerror( $parser ){
                       
                    $error  = xml_error_string ( xml_get_error_code ( $parser ));
                          
                    $errorline = xml_get_current_line_number( $parser );
                          
                    $errorcolumn = xml_get_current_column_number( $parser );
                          
         return "<b>error : $error  at line  $errorline column  $errorcolumn</b>";          

                  }
                        
                        
             // create the parser
                        
             $parser = xml_parser_create();
                        
             xml_parser_set_option($parser, xml_option_case_folding, false);
                        
             // register the event handlers with the parser 
                        
xml_set_element_handler($parser,"startelementhandler","endelementhandler" );
                        
xml_set_character_data_handler( $parser, "characterhandler" );
                        
             // read  and parse the xml document 
                        
             $xml = file_get_contents( ".\stocklist.xml" );
                        
             xml_parse( $parser, $xml ) or die( parseerror ( $parser ));
                        
             xml_parser_free( $parser );
                        
                 ?>
                         
           </pre>
                             
        </body>
                       
    </html>

the output of the code above:

                   start tag : stocklist

                   start tag : item attributes :-  type=fruit
                   
                   start tag : name
                   
                   character data: apple
                   
                   end of element : name
 
                   start tag : price
                   
                   character data: 12.12
                   
                   end of element : price
                   
                   start tag : quantity
                   
                   character data: 220
                   
                   end of element : quantity
                   
                   end of element : item
                   
                   end of element : stocklist