Example PHP to Parse XML

⟵ Back to XML Help

The PHP code below is an example script for parsing the XML request sent for generating registration keys.

<?php
class BMTXMLParser {
   var $tag_name;
   var $tag_data;
   var $tag_prev_name;
   var $tag_parent_name;
   function BMTXMLParser () {
       $tag_name = NULL;
       $tag_data = array ();
       $tag_prev_name = NULL;
       $tag_parent_name = NULL;
       }
   function startElement ($parser, $name, $attrs) {
      if ($this->tag_name != NULL) {
         $this->tag_parent_name = $this->tag_name;
         }                
      $this->tag_name = $name;
      }
   function endElement ($parser, $name) {
      if ($this->tag_name == NULL) {
         $this->tag_parent_name = NULL;
         }
      $this->tag_name = NULL;
      $this->tag_prev_name = NULL;
      }
   function characterData ($parser, $data) {
      if ($this->tag_name == $this->tag_prev_name) {
         $data = $this->tag_data[$this->tag_name] . $data;
         }
      $this->tag_data[$this->tag_name] = $data;
      if ($this->tag_parent_name != NULL) {
         $this->tag_data[$this->tag_parent_name . "." . $this->tag_name] = $data;
         }
      $this->tag_prev_name = $this->tag_name;
      }
   function parse ($data) {
      if (!function_exists ('xml_parser_create')) {
         $this->tag_data['error'] = 'php-xml is not installed on the server';
         return (false);
         }
      $xml_parser = xml_parser_create ();
      xml_set_object ($xml_parser, $this);                        
      xml_parser_set_option ($xml_parser, XML_OPTION_CASE_FOLDING, false);
      xml_set_element_handler ($xml_parser, "startElement", "endElement");
      xml_set_character_data_handler ($xml_parser, "characterData");
      $success = xml_parse ($xml_parser, $data, true);
      if (!$success) {
          $this->tag_data['error'] =  sprintf ("XML error: %s at line %d", xml_error_string(xml_get_error_code ($xml_parser)), xml_get_current_line_number ($xml_parser));
          }
      xml_parser_free ($xml_parser);
      return ($success);
      }
   function getElement ($tag) {
      return ($this->tag_data[$tag]);
      }
   }

header ("Content-type: text/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<response>';
echo '<registrationkey>';
$bmtparser = new BMTXMLParser ();
$postdata = file_get_contents ("php://input"); 
if ($bmtparser->parse ($postdata)) {   
   # keycount is normally 1. However, if the product option "Use one
   # key" in the vendor area has been unchecked, BMT Micro expects
   # you to send back as many keys as the number of items (quantity)
   # ordered. The variable keycount represents the number of keys
   # that the system expects to receive back from you.
   $keycount = $bmtparser->getElement ('keycount');
   for ($key = 1; $key <= $keycount; $key++) {
      $keydata = 'The registration key for ' . $bmtparser->getElement ('registername') . ' is ' . $key;
      echo '<keydata>' . $keydata . '</keydata>';
      }
   }
else {
   echo '<errorcode>1</errorcode>';
   echo '<errormessage>' . $bmtparser->getElement ('error') . '</errormessage>';
   }
echo '</registrationkey>';
echo '</response>';
?>


⟵ Back to XML Help

Skip to toolbar