Re: 'XML To Array' Class

From: Date: Mon, 23 Apr 2001 14:19:33 +0000
Subject: Re: 'XML To Array' Class
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-49854@lists.php.net to get a copy of this message
heey, i'm currently facing the same problem as you. i was thinking of using either xml_parse_into_struct or using a class i found phpxml.class at http://www.phpxml.org let me know how you did it, i'm very intrested Serge "Zeddicus Zu'l Zorandre" Vleugels -- freedom is the wizard's only choice -- On 23 Apr 2001 09:18:25 -0400, php@swifte.net wrote: > 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/ > > -- > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > >

« previous php.general (#49854) next »