XML to array
Christian W.
- php
0 Julian von Mendel0 Andreas Korthaus0 dedlfix0 fk
Hallo,
ich moechte eine XML Datei in ein Arry umwandeln. Leider bekomme ich das nicht hin auch nach jetzt schon 3 Std internet suche. Vielleicht kann mir ja jemand weiterhelfen.
So sieht die XML Datei aus (Bsp.):
<?xml version="1.0" encoding="ISO-8859-1" ?>
<contain>
<title>XMLFile</title>
<test>
<item>content</item>
<item2>
<new></new>
<new2>irgendwas</new2>
</item2>
</test>
<logo></logo>
</contain>
und so moechte ich das Array haben:
(na ja, ihr wisst schon die "" durch [] ersetzt)
"contain" = array("title" => "XMLFile",
"test" => array("item" => "content",
"item2" => array("new" => "",
"new2" => "irgendwas",
),
),
"logo" => "",
);
Hallo Christian,
schau dir mal miniXML an, damit ist das ganz primitiv: http://minixml.psychogenic.com/. Mit fromString/fromFile und toArray solltest du ziemlich schnell dein Ziel erreichen.
Schöne Grüße
Julian
Hi Julian,
danke fuer deine Tip.
Leider scheint bei sourceforge im Moment der Teufel los zu sein, daher konnte ich's noch nicht downloaden und ausprobieren.
Melde mich aber dann wieder wenn geklappt hat.
Der download hat nun endlich geklappt!!
Nach ein paar testlaeufen mus ich aber leider sagen, dass mir der umfang von minixml zu gross, fuer mein kleines projekt, ist. (ok und zugegeben ich habs auch nicht so hingekriegt wie ich es wollte)
Dennoch vielen Dank und ein guts naechtle
Gruesse
Christian W.
Hallo,
ich moechte eine XML Datei in ein Arry umwandeln.
vielleicht hilft Dir ja simplexml - man benötigt allerdings php5 - was die meisten größeren Provider inzwischen aber anbieten sollten.
Noch zwei Links mit zusätzlichen Beispielen:
http://www.zend.com/php5/articles/php5-simplexml.php
http://www.zend.com/php5/abs/php101-11.php
Grüße
Andreas
ich moechte eine XML Datei in ein Arry umwandeln. Leider bekomme ich das nicht hin auch nach jetzt schon 3 Std internet suche. Vielleicht kann mir ja jemand weiterhelfen.
Such mal in deiner eigenen PHP-Installation nach PEAR. Dann musst du dir noch das Package XML_Serializer ziehen und kannst den darin enthaltenen XML_Unserializer benutzen. (Ist zwar als beta gekennzeichnet, funktioniert aber gut.)
ich bin mal so nett aus meinem bastelfundus:
/**
* Diese Klasse erstellt ein array aus einem XML Datenstrom
*
* Ausgabe:
* [TAG1][TAG2][<ATTRIBUTES>] = Attribute des Tags(array)
* [TAG1][TAG2][<CDATA>] = Text
* oder XPATH
* [TAG1.TAG2.<ATTRIBUTES>] = Attribute des Tags(array)
* [TAG1.TAG2.<CDATA>] = Text
* oder gemischt
*
* Geistiges Eigentum von XXXXXXXXXXXXX - Internetprogrammierung - .
* Die Weitergabe ist nur mit diesem Kommentar erlaubt.
* Änderungen Vorbehalten
*
* @access public
* @author XXXXXXXXXXXXXXXX
* @copyright XXXXXXXXXXXXXXXX - Internetprogrammierung - .
* @package Utilities
* @version 0.1
*/
class xml2array {
//
var $Ausgabe = array();
var $a = array();
var $parser;
var $Pfad = '';
var $tags = array();
//
function xml2array() {
echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.')['.__LINE__.'] -->'."\r\n");
//$this->parser = $this->xml_parser_create();
$this->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');
xml_set_default_handler($this->parser, '_sonst');
$this->a = &$this->Ausgabe;
}
//
function &parse($data) {
echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$data.')['.__LINE__.'] -->'."\r\n");
$this->Ausgabe = array();
$this->a = &$this->Ausgabe;
$this->parser;
$this->Pfad = '';
$this->tags = array();
if ($result = xml_parse($this->parser, $data)) {
echo('<!-- '.__FILE__.'['.__LINE__.']'."\r\n");
echo('return: '); print_r($this->Ausgabe); echo("\r\n");
echo(__FILE__.'['.__LINE__.']'.' -->'."\r\n");
return $this->Ausgabe;
}
else {
return $this->parser;
}
}
//
function &xml_parser_create($Zeichensatz = NULL) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.')['.__LINE__.'] -->'."\r\n");
return $this->parser = xml_parser_create($Zeichensatz);
}
//
function &xml_parser_free($parser = NULL) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.')['.__LINE__.'] -->'."\r\n");
if (isset($parser)) {
return xml_parser_free($parser);
}
else {
return xml_parser_free($this->parser);
}
}
//
function &xml_get_error_code($parser = NULL) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.')['.__LINE__.'] -->'."\r\n");
if (isset($parser)) {
return xml_get_error_code($parser);
}
else {
return xml_get_error_code($this->parser);
}
}
//
function &xml_error_string($code = NULL) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$code.')['.__LINE__.'] -->'."\r\n");
if (isset($code)) {
return xml_error_string($code);
}
else {
return xml_error_string($this->xml_get_error_code());
}
}
//
function _tag_open($parser, $tag, $attributes) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$tag.', '.$attributes.')['.__LINE__.'] -->'."\r\n");
$this->Pfad = join('.', $this->tags);
if ($this->Pfad <> '') {
$this->a = &$this->Ausgabe[$this->Pfad];
}
else {
$this->a = &$this->Ausgabe;
}
if (!isset($this->a[$tag])) {
$this->a[$tag] = array();
}
$i = count($this->a[$tag]);
$this->a[$tag][$i] = array();
$this->a[$tag][$i]['<ATTRIBUTES>'] = $attributes;
$this->a[$tag][$i]['<CDATA>'] = '';
$this->a[$tag.'['.$i.'].<ATTRIBUTES>'] = &$this->a[$tag][$i]['<ATTRIBUTES>'];
$this->a[$tag.'['.$i.'].<CDATA>'] = &$this->a[$tag][$i]['<CDATA>'];
$this->tags[count($this->tags)] = $tag.'['.$i.']';
$this->Pfad = join('.', $this->tags);
$this->Ausgabe[$this->Pfad] = &$this->a[$tag][$i];
$this->a = &$this->Ausgabe[$this->Pfad];
$this->Ausgabe[$this->Pfad.'.<ATTRIBUTES>'] = &$this->a['<ATTRIBUTES>'];
$this->Ausgabe[$this->Pfad.'.<CDATA>'] = &$this->a['<CDATA>'];
}
//
function _cdata($parser, $cdata) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$cdata.')['.__LINE__.'] -->'."\r\n");
$this->a['<CDATA>'] .= $cdata;
}
//
function _tag_close($parser, $tag) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$tag.')['.__LINE__.'] -->'."\r\n");
unset ($this->tags[count($this->tags)-1]);
$this->Pfad = join('.', $this->tags);
$this->a = &$this->Ausgabe[$this->Pfad];
}
//
function _sonst($parser, $sonst) {
//echo('<!-- '.__CLASS__.'->'.__FUNCTION__.'('.$parser.', '.$sonst.')['.__LINE__.'] -->'."\r\n");
return;
echo('<!-- '.__FILE__.'['.__LINE__.']'."\r\n");
echo('sonst: '); print_r($sonst); echo("\r\n");
echo(__FILE__.'['.__LINE__.']'.' -->'."\r\n");
}
//
}
benutzung: (nur als beispiel)
unset($xml2array);
$xml2array = new xml2array();
echo('<!-- '.__FILE__.'['.__LINE__.'] -->'."\r\n");
$a = $xml2array->parse($UserCheck);
echo('<!-- '.__FILE__.'['.__LINE__.'] a: '.$a.' -->'."\r\n");
if (is_array($a)) {
//echo('a: '); var_dump($a);
$userdata = &$a['XICONTAINER'][0]['XIRESULT'][0]['RESULTUSERCHECK'][0];
$userdata = &$a['XICONTAINER[0].XIRESULT[0].RESULTUSERCHECK[0]'];
//echo('userdata: '); var_dump($userdata);
echo('Auswahlstufe: '); echo($userdata['BOOLEAN[0].<CDATA>']); echo ' ';
}
else {
//echo (xml_error_string(xml_get_error_code($a)));
echo ($xml2array->xml_get_error_code());
echo ': ';
echo ($xml2array->xml_error_string($xml2array->xml_get_error_code()));
}