Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

The new xml parsing class

  • Loading branch information...
commit f8a6a83698e215654c248af006abd093b540eab1 1 parent 28a774b
authored

Showing 1 changed file with 56 additions and 0 deletions. Show diff stats Hide diff stats

  1. 56  mod/scorm/xml2array.class.php
56  mod/scorm/xml2array.class.php
... ...
@@ -0,0 +1,56 @@
  1
+<?php
  2
+/* Usage
  3
+ Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
  4
+
  5
+ $objXML = new xml2Array();
  6
+ $arrOutput = $objXML->parse($strYourXML);
  7
+ print_r($arrOutput); //print it out, or do whatever!
  8
+  
  9
+*/
  10
+class xml2Array {
  11
+   
  12
+   var $arrOutput = array();
  13
+   var $resParser;
  14
+   var $strXmlData;
  15
+   
  16
+   function parse($strInputXML) {
  17
+   
  18
+           $this->resParser = xml_parser_create ('UTF-8');
  19
+           xml_set_object($this->resParser,$this);
  20
+           xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
  21
+           
  22
+           xml_set_character_data_handler($this->resParser, "tagData");
  23
+       
  24
+           $this->strXmlData = xml_parse($this->resParser,$strInputXML );
  25
+           if(!$this->strXmlData) {
  26
+               die(sprintf("XML error: %s at line %d",
  27
+                           xml_error_string(xml_get_error_code($this->resParser)),
  28
+                           xml_get_current_line_number($this->resParser)));
  29
+           }
  30
+                           
  31
+           xml_parser_free($this->resParser);
  32
+           
  33
+           return $this->arrOutput;
  34
+   }
  35
+   
  36
+   function tagOpen($parser, $name, $attrs) {
  37
+       $tag=array("name"=>$name,"attrs"=>$attrs); 
  38
+       array_push($this->arrOutput,$tag);
  39
+   }
  40
+   
  41
+   function tagData($parser, $tagData) {
  42
+       if(trim($tagData)) {
  43
+           if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
  44
+               $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= utf8_decode($tagData);
  45
+           } else {
  46
+               $this->arrOutput[count($this->arrOutput)-1]['tagData'] = utf8_decode($tagData);
  47
+           }
  48
+       }
  49
+   }
  50
+   
  51
+   function tagClosed($parser, $name) {
  52
+       $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
  53
+       array_pop($this->arrOutput);
  54
+   }
  55
+}
  56
+?>

0 notes on commit f8a6a83

Please sign in to comment.
Something went wrong with that request. Please try again.