Begin parsing xml data.

Info tags.
This commit is contained in:
stronk7
2003-05-16 23:57:35 +00:00
parent d348c53f1f
commit 785ae439eb
2 changed files with 97 additions and 2 deletions
+9 -1
View File
@@ -59,8 +59,16 @@
//Now check for the moodle.xml file
if ($status) {
$xml_file = $CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml";
echo "<li>Checking backup file";
$status = restore_check_moodle_file ($CFG->dataroot."/temp/backup/".$backup_unique_code."/moodle.xml");
$status = restore_check_moodle_file ($xml_file);
}
//Now read the info tag (all)
if ($status) {
echo "<li>Reading info from file";
//Reading info from file
$info = restore_read_xml_info ($xml_file);
}
//End the main ul
+88 -1
View File
@@ -1,7 +1,7 @@
<?PHP //$Id$
//Functions used in restore
//This function unzips a zip file in the samen directory that it is
//This function unzips a zip file in the same directory that it is
//It automatically uses pclzip or command line unzip
function restore_unzip ($file,$moodle_home) {
@@ -46,4 +46,91 @@
return $status;
}
//This function read the xml file and store it data from the info section in an array
function restore_read_xml_info ($xml_file) {
//We call the main read_xml function, with todo = INFO
$info = restore_read_xml ($xml_file,"INFO",false);
return $info;
echo "finished";
}
//=====================================================================================
//== ==
//== XML Functions (SAX) ==
//== ==
//=====================================================================================
//This is the class used to do all the xml parse
class MoodleParser {
var $level = 0; //Level we are
var $tree = array(); //Array of levels we are
var $content = ""; //Content under current level
var $todo = ""; //What we hav to do when parsing
var $info = array(); //Information collected (todo = INFO)
var $preferences = ""; //Preferences about what to load !!
var $finished = false; //Flag to say xml_parse to stop
function startElement($parser, $tagName, $attrs) {
$this->tree[$this->level] = $tagName;
//echo str_repeat("&nbsp;",$this->level*2)."&lt;".$tagName."&gt;<br>\n";
$this->level++;
}
function endElement($parser, $tagName) {
if (trim($this->content)) {
//echo utf8_decode(str_repeat("&nbsp;",$this->level*2).$this->content."<br>\n");
}
$this->level--;
//echo str_repeat("&nbsp;",$this->level*2)."&lt;/".$tagName."&gt;<br>\n";
$this->tree[$this->level] = "";
$this->content = "";
//Stop parsing if todo = INFO and tagName = INFO
//Speed up a lot (avoid parse all)
//echo $tagName."<br>";
if (($this->todo == "INFO") and ($tagName == "INFO")) {
$this->finished = true;
}
}
function characterData($parser, $data) {
$this->content .= $data;
}
}
//This function executes the MoodleParser
function restore_read_xml ($xml_file,$todo,$preferences) {
$status = true;
$xml_parser = xml_parser_create();
$moodle_parser = new MoodleParser();
$moodle_parser->todo = $todo;
$moodle_parser->preferences = $preferences;
xml_set_object($xml_parser,&$moodle_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen($xml_file,"r")
or $status = false;
if ($status) {
while ($data = fread($fp, 4096) and !$moodle_parser->finished)
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
}
xml_parser_free($xml_parser);
if ($Status) {
return $info;
} else {
return $status;
}
}
?>