Location: PHPKode > scripts > DaocGuildXML > daocguildxml/daoc_guild_xml.class.php
<?php

/* ------------------------------------------------------------------------

  Class:        DaocGuildXML
  Author:       Brian Murphy (http://www.bigmurph.com)
  Date:         5/26/2002
  Version:      1.00beta

  Change Log:
    
    Version     Date            Name        Description
    -------     ----------      -------     ---------------
    1.00b       2002-05-26      bmurphy     Initial Release

  Description:  

    This class will parse a guild's xml file, and save it to an internal 
    hash array structure.  The class is really simple to use.
    There is a demo of this class at:
        http://bigmurph.com/daoc/daoc_guild_test.php

  Minimal Working Example: 

    // Include the Class File
    include_once('daoc_guild_xml.class.php');

    // Set the file name and create the guild object
    $file = 'http://www.camelotherald.com/guilds/Igraine/517.xml';

    // Create the guild object
    $guild = new DaocGuildXML();
    $guild->setFileName($file);

    // Parse the file
    $guild->parseFile();

    // Dump the tree
    $guild->DataDumper():

------------------------------------------------------------------------ */

class DaocGuildXML {

    // Public Member Variables
    var $tree = array();
    var $member_names = array();
    var $member_attr  = array();

    // Constructor
    function DaocGuildXML($file = '') {

        // Optionally set the file name
        $this->file = $file;
    }

    // Set the file name with this function or in the constructor
    function setFileName($file) {

        // Set the File name
        $this->file = $file;
    }

    // Parse the XML File
    function parseFile() {
        if (!(list($this->_xml_parser, $fp) = $this->_new_xml_parser($this->file))) {
           die("could not open XML input");
        }
        while ($data = fread($fp, 4096)) {
           if (!xml_parse($this->_xml_parser, $data, feof($fp))) {
               die(sprintf("XML error: %s at line %d\n",
                           xml_error_string(xml_get_error_code($this->_xml_parser)),
                           xml_get_current_line_number($this->_xml_parser)));
           }
        }
        xml_parser_free($this->_xml_parser);

        // Build member information for acceessor funcs
        $this->_buildMemberInformation();
    }

    function DataDumper() {
        print "<pre><div style=\"font-weight:normal;font-family:Courier New;\" align=left>";
        print_r($this->tree);
        print "</div></pre>";
    }

    // Returns an array of Character Names in the file
    function getCharNames() {
        return $this->member_names;
    }

    // Returns an array of Available Attributes, this is needed because
    // of characters having ANON set to false so you crafting information.
    function getCharAttributes() {
        return $this->member_attr;
    }

    // --------------------- Private Functions --------------------- //
    var $_char_name;
    var $_char_attrib;
    var $_xml_parser;

    function _trustedFile() {
       if (!eregi("^([a-z]+)://", $this->file) 
           && fileowner($this->file) == getmyuid()) {
               return true;
       }
       return false;
    }

    function _startElement($parser, $name, $attribs) {

       if ($name == "CHARACTER") {
          $this->_char_name = $attribs["NAME"];
          $this->tree["MEMBERS"][$this->_char_name]["LASTON"] = $attribs["LASTON"];
       }
       else if ($name == "GUILD") {
           if (sizeof($attribs)) {
               while (list($k, $v) = each($attribs)) {
                   $this->tree["GUILD"][$k] = $v; } }
       }
       else if ($this->_char_name != "") {
          $this->_char_attrib = $name;
       }
    }

    function _endElement($parser, $name) {
       
       if ($name == "CHARACTER") {
            $this->_char_name = "";
       }
       if ($name == $this->_char_attrib) {
            $this->_char_attrib = "";
       }
    }

    function _characterData($parser, $data) {

       if($this->_char_attrib != "") {
          $this->tree["MEMBERS"][$this->_char_name][$this->_char_attrib] = $data;
       }
    }

    function _PIHandler($parser, $target, $data) {
       switch (strtolower($target)) {
           case "php":
               global $parser_file;
               // If the parsed document is "trusted", we say it is safe
               // to execute PHP code inside it.  If not, display the code
               // instead.
               if (_trustedFile($parser_file[$parser])) {
                   eval($data);
               } else {
                   printf("Untrusted PHP code: <i>%s</i>", 
                           htmlspecialchars($data));
               }
               break;
       }
    }

    function _defaultHandler($parser, $data) {
        // NOT USED
    }

    function _new_xml_parser($file) {
       global $parser_file;
       $this->_xml_parser = xml_parser_create();
       xml_set_object($this->_xml_parser, &$this);
       xml_parser_set_option($this->_xml_parser, XML_OPTION_CASE_FOLDING, 1);
       xml_set_element_handler($this->_xml_parser, "_startElement", "_endElement");
       xml_set_character_data_handler($this->_xml_parser, "_characterData");
       xml_set_processing_instruction_handler($this->_xml_parser, "_PIHandler");
       xml_set_default_handler($this->_xml_parser, "_defaultHandler");
       xml_set_external_entity_ref_handler($this->_xml_parser, "_externalEntityRefHandler");
       if (!($fp = @fopen($file, "r"))) {
           return false;
       }
       if (!is_array($parser_file)) {
           settype($parser_file, "array");
       }
       $parser_file[$this->_xml_parser] = $file;
       return array($this->_xml_parser, $fp);
    }

    function _externalEntityRefHandler($parser, $openEntityNames, $base, $systemId,
                                     $publicId) {
       if ($systemId) {
           if (!list($parser, $fp) = $this->_new_xml_parser($systemId)) {
               printf("Could not open entity %s at %s\n", $openEntityNames,
                      $systemId);
               return false;
           }
           while ($data = fread($fp, 4096)) {
               if (!xml_parse($parser, $data, feof($fp))) {
                   printf("XML error: %s at line %d while parsing entity %s\n",
                          xml_error_string(xml_get_error_code($parser)),
                          xml_get_current_line_number($parser), $openEntityNames);
                   xml_parser_free($parser);
                   return false;
               }
           }
           xml_parser_free($parser);
           return true;
       }
       return false;
    }

    function _buildMemberInformation() {
        foreach ($this->tree["MEMBERS"] as $name => $prop) {
            array_push($this->member_names, $name);
            foreach ($prop as $key => $value) {
                if(!in_array($key, $this->member_attr)) {
                    array_push($this->member_attr, $key);
                }
            }
        }
    }

} // End Class DaocGuildXML
?>
Return current item: DaocGuildXML