'XML To Array' Class

From: Date: Mon, 23 Apr 2001 13:18:25 +0000
Subject: 'XML To Array' Class
Groups: php.general 
Request: Send a blank email to php-general+get-49850@lists.php.net to get a copy of this message
Hello, I'm new to using XML with PHP, but I think I've got it figured out for the most part. I have created a class that reads in an XML file and returns the data it finds to an array, but I'm running into a problem because the array keeps coming back empty. Would you mind taking a moment to see where I'm going wrong? file.xml ======================================================================= <ARTICLE> <URL>/article1.html</URL> <TITLE>Article 1</TITLE> </ARTICLE> <ATRICLE> <URL>/article2.html</URL> <TITLE>Article 2</TITLE> </ATRICLE> xml.class.php ======================================================================= <? class eXML { var $Parser; var $theArray; var $theArrayTopElement; var $theArrayElements; var $theArrayPointer; var $theArrayGetElement; function eXML($_PARENT_, $_CHILDREN_) { $this->Parser = xml_parser_create("ISO-8859-1"); xml_set_object($this->Parser, &$this); xml_set_element_handler($this- >Parser, "Tag_Open", "Tag_Close"); xml_set_character_data_handler($this- >Parser, "CData"); $this->theArray = array(); $this->theArrayPointer = 0; $this->theArrayGetElement = NULL; $this->theArrayTopElement = $_PARENT_; $this->theArrayElements = $_CHILDREN_; } function Free() { xml_parser_free($this->Parser); } function Parse_Array($_FILE_) { $_FP_ = fopen($_FILE_, "r") or die("Cannot Open XML Stream"); while ($_DATA_ = fread($_FP_, 4096)) { if (!xml_parse($this->Parser, $_DATA_, feof($_FP_))) { return(FALSE); } } fclose($_FP_); var_dump($this->theArray); return($this->theArray); } function Tag_Open($_PARSER_, $_TAG_, $_ATTR_) { if ($_TAG_ == $this->theArrayTopElement) { $this->theArray[$this->theArrayPointer] = "SOMETEXT"; // NOT SURE IF THIS IS NECESSARY } $_ELEMENTS_ = explode("::", $this- >theArrayElements); for ($I = 0; $I < count($_ELEMENTS_); $I++) { if ($_TAG_ == $_ELEMENTS_[$I]) { $this->theArrayGetElement = $_ELEMENTS_[$I]; } } } function Tag_Close($_PARSER_, $_TAG_) { if ($_TAG_ == $this->theArrayTopElement) { $this->theArrayPointer++; } $this->theArrayGetElement = NULL; } function CData($_PARSER_, $_CDATA_) { if ($this->theArrayGetElement != NULL) { array_push($this->theArray, $_CDATA_); $this->theArray[$this->theArrayPointer] [$this->theArrayGetElement] = $_CDATA_; } } } ?> xml.test.php ======================================================================= <? $XML = new eXML("ARTICLE", "URL::TITLE"); $ARTICLES = $XML->Parse_Array("file.xml"); print_r($ARTICLES); ?> What I expect to happen is this: $ARTICLES[0]["URL"] == "/article1.html"; $ARTICLES[0]["TITLE"] == "Article 1"; $ARTICLES[1]["URL"] == "/article2.html"; $ARTICLES[1]["TITLE"] == "Article 2"; But the array is empty. Thankz in advance for your help. Robert -- [ Swift eNetwork ] Matrix http://matrix.swifte.net/ --

« previous php.general (#49850) next »