xml.inc.php 2.18 KB
Newer Older
Ayolo's avatar
Ayolo committed
1 2 3 4 5 6 7 8 9 10 11
<?php

/*
 * Public domain.
 */

class u007xml
{
   var $arrOutput = array();
   var $resParser;
   var $strXmlData;
12 13


Ayolo's avatar
Ayolo committed
14 15 16 17 18 19 20 21 22 23
   function u007xml($tdata = "")
   {
       return $this->parse($tdata);
   }

   function parse($strInputXML)
   {
       $this->resParser = xml_parser_create ();
       xml_set_object($this->resParser,$this);
       xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
24

Ayolo's avatar
Ayolo committed
25
       xml_set_character_data_handler($this->resParser, "tagData");
26

Ayolo's avatar
Ayolo committed
27
       $this->strXmlData = xml_parse($this->resParser,$strInputXML );
28

Ayolo's avatar
Ayolo committed
29 30 31 32 33
       if(!$this->strXmlData) {
           die(sprintf("XML error: %s at line %d",
       xml_error_string(xml_get_error_code($this->resParser)),
       xml_get_current_line_number($this->resParser)));
       }
34

Ayolo's avatar
Ayolo committed
35
       xml_parser_free($this->resParser);
36

Ayolo's avatar
Ayolo committed
37 38
       return $this->arrOutput;
   }
39

Ayolo's avatar
Ayolo committed
40 41 42 43 44
   //called on each xml tree
   function tagOpen($parser, $name, $attrs) {
       $tag=array("nodename"=>$name,"attributes"=>$attrs);
       array_push($this->arrOutput,$tag);
   }
45

Ayolo's avatar
Ayolo committed
46 47 48 49 50 51 52 53 54 55 56
  //called on data for xml
   function tagData($parser, $tagData) {
       if(trim($tagData)) {
           if(isset($this->arrOutput[count($this->arrOutput)-1]['nodevalue'])) {
               $this->arrOutput[count($this->arrOutput)-1]['nodevalue'] .= $this->parseXMLValue($tagData);
           }
           else {
               $this->arrOutput[count($this->arrOutput)-1]['nodevalue'] = $this->parseXMLValue($tagData);
           }
       }
   }
57

Ayolo's avatar
Ayolo committed
58 59 60
  //called when finished parsing
   function tagClosed($parser, $name) {
       $this->arrOutput[count($this->arrOutput)-2]['childrens'][] = $this->arrOutput[count($this->arrOutput)-1];
61

Ayolo's avatar
Ayolo committed
62 63 64 65 66 67 68 69 70 71 72
       if(count ($this->arrOutput[count($this->arrOutput)-2]['childrens'] ) == 1)
       {
           $this->arrOutput[count($this->arrOutput)-2]['firstchild'] =& $this->arrOutput[count($this->arrOutput)-2]['childrens'][0];
       }
       array_pop($this->arrOutput);
   }

   function toArray()
   {
       //not used, we can call loadString or loadFile instead...
   }
73

Ayolo's avatar
Ayolo committed
74 75 76 77 78
   function parseXMLValue($tvalue)
   {
       //$tvalue = htmlentities($tvalue,ENT_NOQUOTES,"UTF-8");
       return $tvalue;
   }
79

Ayolo's avatar
Ayolo committed
80 81 82 83 84 85
   function displayXML()
   {
       print_r($this->arrOutput);
   }
}
?>