[289] | 1 | <?php
|
---|
| 2 | #$Id$
|
---|
| 3 |
|
---|
| 4 | /*
|
---|
| 5 | * XML.inc.php
|
---|
| 6 | *
|
---|
| 7 | * Class to convert an XML file into an object
|
---|
| 8 | *
|
---|
| 9 | * Copyright (C) 2006 Oliver Strecke <oliver.strecke@browsertec.de>
|
---|
| 10 | *
|
---|
| 11 | * This library is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 13 | * License as published by the Free Software Foundation; either
|
---|
| 14 | * version 2 of the License, or (at your option) any later version.
|
---|
| 15 | *
|
---|
| 16 | * This library is distributed in the hope that it will be useful,
|
---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | * Lesser General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU Lesser General Public
|
---|
| 22 | * License along with this library; if not, write to the Free Software
|
---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | class Xml{
|
---|
| 27 | var $_parser;
|
---|
| 28 | var $_xml_data;
|
---|
| 29 | var $_actual_tag;
|
---|
| 30 |
|
---|
| 31 | //Constructor...
|
---|
| 32 | function xml($encoding="UTF-8"){
|
---|
| 33 | $this->_parser=xml_parser_create($encoding);
|
---|
| 34 | $this->_xml_data="";
|
---|
| 35 | $this->_actual_tag=$this;
|
---|
| 36 |
|
---|
| 37 | xml_set_object($this->_parser,$this);
|
---|
| 38 | xml_parser_set_option($this->_parser,XML_OPTION_CASE_FOLDING,false);
|
---|
| 39 | xml_set_element_handler($this->_parser,"tag_open","tag_close");
|
---|
| 40 | xml_set_character_data_handler($this->_parser,"tag_data");
|
---|
| 41 | xml_set_default_handler($this->_parser,"tag_data");
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | //get XML data from file...
|
---|
| 45 | function file_read($xml_file){
|
---|
| 46 | if(file_exists($xml_file)){
|
---|
| 47 | $this->_xml_data=implode("",file($xml_file));
|
---|
| 48 | return 1;
|
---|
| 49 | }else{
|
---|
| 50 | return 0;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | //parse XML data...
|
---|
| 56 | function parse($xml_data=0){
|
---|
| 57 | if($xml_data)$this->_xml_data=$xml_data;
|
---|
| 58 | xml_parse($this->_parser,$this->_xml_data);
|
---|
| 59 | xml_parser_free($this->_parser);
|
---|
| 60 | return 1;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | function tag_open($parser,$name,$attrs){
|
---|
| 64 | //create new tag...
|
---|
| 65 | #$actual_tag=&$this->_actual_tag;
|
---|
| 66 | $tag=new XML_TAG($this->_actual_tag);
|
---|
| 67 | $tag->_name=$name;
|
---|
| 68 | $tag->_param=$attrs;
|
---|
| 69 | if($name=="br" && isset($this->_actual_tag->_value))$this->_actual_tag->_value=$this->_actual_tag->_value."\n"; else $this->_actual_tag->_value="\n";
|
---|
| 70 |
|
---|
| 71 | //add tag object to parent/actual tag object...
|
---|
| 72 | #if(is_object($this->_actual_tag)){
|
---|
| 73 | if(!is_a($this->_actual_tag,"XML")){
|
---|
| 74 | #if(is_object($this->_actual_tag->$name) || is_array($this->_actual_tag->$name)){
|
---|
| 75 | if(isset($this->_actual_tag->$name)){
|
---|
| 76 | //same child objects -> Array...
|
---|
| 77 | $last_index=$this->_actual_tag->new_child_array($tag,$name);
|
---|
| 78 | $this->_actual_tag=&$this->_actual_tag->{$name}[$last_index];
|
---|
| 79 | }else{
|
---|
| 80 | //add new child object to actual tag...
|
---|
| 81 | $this->_actual_tag->new_child($tag,$name);
|
---|
| 82 | $this->_actual_tag=&$this->_actual_tag->$name;
|
---|
| 83 | }
|
---|
| 84 | }else{
|
---|
| 85 | //copy first tag object in this object...
|
---|
| 86 | $this->$name=$tag;
|
---|
| 87 | $this->_actual_tag=&$this->{$name};
|
---|
| 88 | }
|
---|
| 89 | return 1;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | function tag_data($parser,$string){
|
---|
| 93 | if(strlen(trim($string))>0){
|
---|
| 94 | if(isset($this->_actual_tag->_value))$this->_actual_tag->_value=$this->_actual_tag->_value.$string; else $this->_actual_tag->_value=$string;
|
---|
| 95 | }
|
---|
| 96 | return 1;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | function tag_close($parser,$name){
|
---|
| 100 | $this->_actual_tag=&$this->_actual_tag->_parent;
|
---|
| 101 | return 1;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | function file_write($xml_file){
|
---|
| 105 | $fp=fopen($xml_file,"w");
|
---|
| 106 | preg_match_all("/\<\?xml(.*)\?\>/",$this->_xml_data,$result_array);
|
---|
| 107 | if(is_array($result_array)){
|
---|
| 108 | foreach($result_array[1] as $header){
|
---|
| 109 | fputs($fp,"<?xml$header?>\n");
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | $this->tag_write($fp,$this);
|
---|
| 113 | fclose($fp);
|
---|
| 114 | return 1;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | function tag_write($fp,$tag,$indent=0){
|
---|
| 118 | $return=0;
|
---|
| 119 | $tmp_array=get_object_vars($tag);
|
---|
| 120 | $indent_string="";
|
---|
| 121 | for($i=0;$i<$indent;$i++)$indent_string.=" ";
|
---|
| 122 | foreach($tmp_array as $tag_name=>$tag){
|
---|
| 123 | if(is_a($tag,"XML_TAG") && substr($tag_name,0,1)!="_"){
|
---|
| 124 | $return=1;
|
---|
| 125 | fputs($fp,"\n$indent_string<$tag_name");
|
---|
| 126 | foreach($tag->_param as $name=>$value){
|
---|
| 127 | fputs($fp," $name='".htmlentities($value)."'");
|
---|
| 128 | }
|
---|
| 129 | fputs($fp,">");
|
---|
| 130 | $result=$this->tag_write($fp,$tag,$indent+1);
|
---|
| 131 | if($result){
|
---|
| 132 | fputs($fp,"\n");
|
---|
| 133 | fputs($fp,"$indent_string</$tag_name>");
|
---|
| 134 | }else{
|
---|
| 135 | fputs($fp,htmlentities($tag->_value));
|
---|
| 136 | fputs($fp,"</$tag_name>");
|
---|
| 137 | }
|
---|
| 138 | }else if(is_array($tag) && substr($tag_name,0,1)!="_"){
|
---|
| 139 | $return=1;
|
---|
| 140 | foreach($tag as $i=>$tmp_tag){
|
---|
| 141 | fputs($fp,"\n$indent_string<$tag_name");
|
---|
| 142 | foreach($tmp_tag->_param as $name=>$value){
|
---|
| 143 | fputs($fp," $name='".htmlentities($value)."'");
|
---|
| 144 | }
|
---|
| 145 | fputs($fp,">");
|
---|
| 146 | $result=$this->tag_write($fp,$tmp_tag,$indent+1);
|
---|
| 147 | if($result){
|
---|
| 148 | fputs($fp,"\n");
|
---|
| 149 | fputs($fp,"$indent_string</$tag_name>");
|
---|
| 150 | }else{
|
---|
| 151 | fputs($fp,htmlentities($tmp_tag->_value));
|
---|
| 152 | fputs($fp,"</$tag_name>");
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 | return $return;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | //Debug...
|
---|
| 161 | function debug($exit=0){
|
---|
| 162 | echo "<pre>";
|
---|
| 163 | print_r($this);
|
---|
| 164 | echo "</pre>";
|
---|
| 165 | if($exit)exit;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | class XML_TAG{
|
---|
| 170 | var $_parent;
|
---|
| 171 | var $_name;
|
---|
| 172 | var $_value;
|
---|
| 173 | var $_param;
|
---|
| 174 |
|
---|
| 175 | //Constructor...
|
---|
| 176 | function xml_tag(&$parent){
|
---|
| 177 | $this->_parent=&$parent;
|
---|
| 178 | $this->_name="";
|
---|
| 179 | $this->_value=false;
|
---|
| 180 | $this->_param=Array();
|
---|
| 181 | return 1;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | //simply add new child to this object...
|
---|
| 185 | function new_child($child,$child_name){
|
---|
| 186 | if(isset($this->$child_name)){
|
---|
| 187 | $this->new_child_array($child,$child_name);
|
---|
| 188 | }else{
|
---|
| 189 | $this->$child_name=&$child;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | //add child array for more same childs to this object...
|
---|
| 194 | function new_child_array($child,$child_name){
|
---|
| 195 | //create array and set old child object to the first array element...
|
---|
| 196 | if(is_object($this->$child_name)){
|
---|
| 197 | $tmp_obj=$this->$child_name;
|
---|
| 198 | $this->$child_name=Array();
|
---|
| 199 | $this->new_child_array($tmp_obj,$child_name);
|
---|
| 200 | }
|
---|
| 201 | //push child reference into child array...
|
---|
| 202 | $this->{$child_name}[]=&$child;
|
---|
| 203 | $last_index=count($this->$child_name)-1;
|
---|
| 204 | return $last_index;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | //Debug...
|
---|
| 208 | function debug(){
|
---|
| 209 | echo "<pre>";
|
---|
| 210 | print_r($this);
|
---|
| 211 | echo "</pre>";
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | ?> |
---|