Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The new xml parsing class

  • Loading branch information...
commit f8a6a83698e215654c248af006abd093b540eab1 1 parent 28a774b
bobopinna authored
Showing with 56 additions and 0 deletions.
  1. +56 −0 mod/scorm/xml2array.class.php
56 mod/scorm/xml2array.class.php
View
@@ -0,0 +1,56 @@
+<?php
+/* Usage
+ Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
+
+ $objXML = new xml2Array();
+ $arrOutput = $objXML->parse($strYourXML);
+ print_r($arrOutput); //print it out, or do whatever!
+
+*/
+class xml2Array {
+
+ var $arrOutput = array();
+ var $resParser;
+ var $strXmlData;
+
+ function parse($strInputXML) {
+
+ $this->resParser = xml_parser_create ('UTF-8');
+ xml_set_object($this->resParser,$this);
+ xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
+
+ xml_set_character_data_handler($this->resParser, "tagData");
+
+ $this->strXmlData = xml_parse($this->resParser,$strInputXML );
+ 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)));
+ }
+
+ xml_parser_free($this->resParser);
+
+ return $this->arrOutput;
+ }
+
+ function tagOpen($parser, $name, $attrs) {
+ $tag=array("name"=>$name,"attrs"=>$attrs);
+ array_push($this->arrOutput,$tag);
+ }
+
+ function tagData($parser, $tagData) {
+ if(trim($tagData)) {
+ if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
+ $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= utf8_decode($tagData);
+ } else {
+ $this->arrOutput[count($this->arrOutput)-1]['tagData'] = utf8_decode($tagData);
+ }
+ }
+ }
+
+ function tagClosed($parser, $name) {
+ $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
+ array_pop($this->arrOutput);
+ }
+}
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.