Maybe this will help. I just made it: class XMLParser { var $depth = 0; var $parser; var $text; var $cdata = array();
function XMLParser() { $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); xml_set_element_handler($this->parser, 'startElement', 'endElement'); xml_set_character_data_handler($this->parser, 'characterData'); } function parse($xml) { $error = xml_parse($this->parser,$xml,true); if ($error === false) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } $this->text = '$arr = '.substr($this->text, 0, strlen($this->text)-2).";\n"; eval($this->text); return $arr; } function startElement($parser, $name, $attrs) { $attrStr = ''; $this->depth++; $this->cdata[$this->depth] = ''; foreach($attrs as $key => $value) { $attrStr .= "'$key' => '$value', "; } $this->text .= "array(\n'name' => '$name', 'attributes' => array($attrStr), "; } function endElement($parser, $name) { $this->text .= " 'cdata' => '".$this->cdata[$this->depth]."'),\n"; $this->depth--; } function characterData($parser, $data) { $this->cdata[$this->depth] .= $data; } } $parser = new XMLParser(); $arr = $parser->parse(file_get_contents('sample.xml')); print_r($arr); "Victor spång arthursson" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] 2003-11-17 kl. 17.06 skrev Chris Hayes: >> Need to read a xml-file into an array, but searching around I havent >> found a way that's easy and simple… Arent there an easy way in PHP to >> accomplish this? > > have you been at http://se.php.net/xml ? Well, I've, and I also have to say that the XML-support in PHP lacks any usability… 2 pages code later I still cannot get it to work, and the manual is pretty thin on this chapter... For example, I've the following XML: <ingrediens> <ingrediensnummer>1234</ingrediensnummer> <maengde>3,4</maengde> <enhed>kg</enhed> </ingrediens> Here I thought that the startelementfunction should be called upon every start tag, the character_data_handler on every text string and the endelement function on every endelement. But what seems to happen is that the character data handler is called to randomly number of times, and if I output the current element every time it gets called I get the following output for the xml above: ingrediens ingrediens ingrediens ingrediensnummer ingrediensnummer ingrediensnummer ingrediensnummer maengde maengde maengde maengde enhed enhed enhed enhed enhed enhed What I expected to get was: ingrediensnummer maengde enhed So, couldnt anyone please bring some clarity into this matter? Sincerely Victor= -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php