5 PRAX - PHP Record-oriented API for XML
7 Affords a database recordset-like view of an XML document
8 in documents which lend themselves to such interpretation.
10 A port of the Perl XML::RAX module by Robert Hanson
11 (http://search.cpan.org/search?mode=module&query=rax)
12 based on the RAX API created by Sean McGrath
13 (http://www.xml.com/pub/2000/04/26/rax)
15 Copyright (c) 2000 Rael Dornfest <rael@oreilly.com>,
18 License is granted to use or modify this software ("PRAX")
19 for commercial or non-commercial use provided the copyright
20 of the author is preserved in any distributed or derivative
23 XML::RAX Copyright (c) 2000 Robert Hanson. All rights
24 reserved. This program ("XML::RAX") is free software; you
25 can redistribute and/or modify it under the terms of the
26 Perl "Artistic License."
27 (http://www.perl.com/language/misc/Artistic.html)
29 For a usage synopsis, see this distribution's README.txt.
30 Take a gander at sample.php (using sample.xml) for a
33 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED
34 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
37 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43 POSSIBILITY OF SUCH DAMAGE.
47 Slightly modified by Wouter Demuynck (wouter@demuynck.org)
55 $this->record_delim = '';
56 $this->fields = array();
57 $this->records = array();
61 $this->field_data = '';
62 $this->tag_stack = array();
65 $this->rax_opened = 0;
67 $this->version = '0.1';
72 function open ($xml) {
74 $this->debug("open(\"$xml\")");
76 if ($this->rax_opened) return 0;
79 $this->rax_opened = 1;
83 function openfile ($filename) {
85 $this->debug("openfile(\"$filename\")");
87 if ($this->rax_opened) return 0;
89 $fp = fopen($filename, "r");
92 $this->xml_file = $fp;
93 $this->rax_opened = 1;
101 function startparse () {
103 $this->debug("startparse()");
105 $this->parser = xml_parser_create();
107 xml_set_object($this->parser,$this);
108 xml_set_element_handler($this->parser, "startElement", "endElement");
109 xml_set_character_data_handler($this->parser, "characterData");
110 xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
112 if (xml_parse($this->parser, '')) {
113 $this->parse_started = 1;
123 $this->debug("parse()");
125 if (!$this->rax_opened) return 0;
126 if ($this->parse_done) return 0;
128 if (!$this->parse_started)
129 if (!$this->startparse()) return 0;
131 if ($this->xml_file) {
133 $buffer = fread($this->xml_file, 4096);
136 xml_parse( $this->parser, $buffer, feof($this->xml_file) );
138 $this->parse_done = 1;
143 xml_parse($this->parser, $this->xml, 1);
144 $this->parse_done = 1;
151 function startElement($parser, $name, $attrs) {
153 $this->debug("startElement($name)");
155 array_push($this->tag_stack, $name);
157 if ( !$this->in_rec and !strcmp($name, $this->record_delim) ) {
159 $this->rec_lvl = sizeof($this->tag_stack);
160 $this->field_lvl = $this->rec_lvl + 1;
162 else if ( $this->in_rec and sizeof($this->tag_stack) == $this->field_lvl ) {
169 function endElement($parser, $name) {
171 $this->debug("endElement($name)");
173 array_pop($this->tag_stack);
175 if ( $this->in_rec ) {
177 if ( sizeof($this->tag_stack) < $this->rec_lvl ) {
179 array_push( $this->records, new RAX_Record( $this->fields ) );
180 $this->fields = array();
182 else if ( sizeof($this->tag_stack) < $this->field_lvl ) {
184 $this->fields[$name] = $this->field_data;
185 $this->field_data = '';
193 function characterData ($parser, $data) {
195 $this->debug("characterData($data)");
197 if ( $this->in_field )
198 $this->field_data .= $data;
203 function setRecord ($delim) {
205 $this->debug("setRecord($delim)");
207 if ($this->parse_started) return 0;
209 $this->record_delim = $delim;
215 function readRecord () {
217 $this->debug("readRecord()");
219 while ( !sizeof($this->records) and !$this->parse_done ) $this->parse();
221 return array_shift($this->records);
225 function debug ($msg) {
226 if ($this->debug) print "$msg<br />\n";
229 // added by Wouter Demuynck
231 if ($this->rax_opened) {
232 $this->debug("Closing RAX");
234 fclose($this->xml_file);
236 $this->rax_opened = 0;
245 function RAX_Record ( $fields ) {
247 $this->fields = $fields;
253 function getFieldnames () {
255 $this->debug("getFieldnames()");
257 return array_keys( $this->fields );
261 function getField ( $field ) {
263 $this->debug("getField($field)");
265 return trim( $this->fields[$field] );
269 function getFields () {
271 $this->debug("getFields()");
273 return array_values( $this->fields );
279 $this->debug("getFields()");
281 return $this->fields;
285 function debug ($msg) {
286 if ($this->debug) print "$msg<br />\n";