I
i125
Hallo zusammen,
ich hab ein kleines Problem.
Ich hab mir eine XML Datei gebaut und die wollte ich nun auslesen und mit dem Inhalt arbeiten. Das ist soweit kein Problem. Es gibt auch genug XML Parser für PHP 4.x (PHP 5 scheidet leider aus, da ich hier für unser Produktivsystem anpacken müsste.)
Der Code vom XML Parser von php.net
Die XML Datei schaut wie folgt aus:
Der Rückgabe Wert:
Nun mein schwieriges Problem. Da es sich um Objecte handelt, und ich mit sowas auf der Basis noch nicht gearbeitet habe, wie arbeite ich mit diesem Objekten, wie kann ich darauf zugreifen, Ebene für Ebene?
Ich danke euch.
LG Alex
ich hab ein kleines Problem.
Ich hab mir eine XML Datei gebaut und die wollte ich nun auslesen und mit dem Inhalt arbeiten. Das ist soweit kein Problem. Es gibt auch genug XML Parser für PHP 4.x (PHP 5 scheidet leider aus, da ich hier für unser Produktivsystem anpacken müsste.)
Der Code vom XML Parser von php.net
PHP:
class XmlElement {
var $name;
var $attributes;
var $content;
var $children;
};
function xml_to_object($xml) {
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $xml, $tags);
xml_parser_free($parser);
$elements = array(); // the currently filling [child] XmlElement array
$stack = array();
foreach ($tags as $tag) {
$index = count($elements);
if ($tag['type'] == "complete" || $tag['type'] == "open") {
$elements[$index] = new XmlElement;
$elements[$index]->name = $tag['tag'];
$elements[$index]->attributes = $tag['attributes'];
$elements[$index]->content = $tag['value'];
if ($tag['type'] == "open") { // push
$elements[$index]->children = array();
$stack[count($stack)] = &$elements;
$elements = &$elements[$index]->children;
}
}
if ($tag['type'] == "close") { // pop
$elements = &$stack[count($stack) - 1];
unset($stack[count($stack) - 1]);
}
}
return $elements[0]; // the single top-level element
}
Die XML Datei schaut wie folgt aus:
Code:
<document>
<head>
<title>Ich bin der Titel</title>
<template>Ich bin das template</template>
</head>
<wildcard>
<name>SPRUNGMARKE</name>
<value>
<function>
<mysql>
<statement>SELECT code from asdasd wehere id=</statement>
</mysql>
</function>
</value>
</wildcard>
<wildcard>
<name>SPRUNGMARKE2</name>
<value>test</value>
</wildcard>
<wildcard>
<name>SPRUNGMARKE2</name>
<value>
<PHP>_ausgabe</PHP>
</value>
</wildcard>
<fieldset>
<textfield>
<name>Ich bin ein Textfeld</name>
<type>text</type>
<style></style>
<length>8</length>
<tooltip>mmmm</tooltip>
<startvalue>Startwert</startvalue>
<primary>yes</primary>
<editable>yes</editable>
<onload>
<function>
<mysql>
<statement>SELECT code from asdasd wehere id=</statement>
</mysql>
</function>
</onload>
<onsave>
<function>
<mysql>
<statement>SELECT code from asdasd wehere id=</statement>
</mysql>
</function>
</onsave>
<ondelete>
<function>
<mysql>
<statement>SELECT code from asdasd wehere id=</statement>
</mysql>
</function>
</ondelete>
<onerror>Stop</onerror>
</textfield>
</fieldset>
<button>
<name>Ok</name>
<value>send</value>
<action>aktion</action>
<bevorsave>
<function>
<php>
<include>file.php</include>
</php>
</function>
</bevorsave>
<aftersave>
<function>
<php>
<include>file.php</include>
</php>
</function>
</aftersave>
</button>
</document>
Der Rückgabe Wert:
Code:
xmlelement Object
(
[name] => document
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => head
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => title
[attributes] =>
[content] => Ich bin der Titel
[children] =>
)
[1] => xmlelement Object
(
[name] => template
[attributes] =>
[content] => Ich bin das template
[children] =>
)
)
)
[1] => xmlelement Object
(
[name] => wildcard
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => name
[attributes] =>
[content] => SPRUNGMARKE
[children] =>
)
[1] => xmlelement Object
(
[name] => value
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => function
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => mysql
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => statement
[attributes] =>
[content] => SELECT code from asdasd wehere id=
[children] =>
)
)
)
)
)
)
)
)
)
[2] => xmlelement Object
(
[name] => wildcard
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => name
[attributes] =>
[content] => SPRUNGMARKE2
[children] =>
)
[1] => xmlelement Object
(
[name] => value
[attributes] =>
[content] => test
[children] =>
)
)
)
[3] => xmlelement Object
(
[name] => wildcard
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => name
[attributes] =>
[content] => SPRUNGMARKE2
[children] =>
)
[1] => xmlelement Object
(
[name] => value
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => PHP
[attributes] =>
[content] => _ausgabe
[children] =>
)
)
)
)
)
[4] => xmlelement Object
(
[name] => fieldset
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => textfield
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => name
[attributes] =>
[content] => Ich bin ein Textfeld
[children] =>
)
[1] => xmlelement Object
(
[name] => type
[attributes] =>
[content] => text
[children] =>
)
[2] => xmlelement Object
(
[name] => style
[attributes] =>
[content] =>
[children] =>
)
[3] => xmlelement Object
(
[name] => length
[attributes] =>
[content] => 8
[children] =>
)
[4] => xmlelement Object
(
[name] => tooltip
[attributes] =>
[content] => mmmm
[children] =>
)
[5] => xmlelement Object
(
[name] => startvalue
[attributes] =>
[content] => Startwert
[children] =>
)
[6] => xmlelement Object
(
[name] => primary
[attributes] =>
[content] => yes
[children] =>
)
[7] => xmlelement Object
(
[name] => editable
[attributes] =>
[content] => yes
[children] =>
)
[8] => xmlelement Object
(
[name] => onload
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => function
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => mysql
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => statement
[attributes] =>
[content] => SELECT code from asdasd wehere id=
[children] =>
)
)
)
)
)
)
)
[9] => xmlelement Object
(
[name] => onsave
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => function
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => mysql
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => statement
[attributes] =>
[content] => SELECT code from asdasd wehere id=
[children] =>
)
)
)
)
)
)
)
[10] => xmlelement Object
(
[name] => ondelete
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => function
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => mysql
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => statement
[attributes] =>
[content] => SELECT code from asdasd wehere id=
[children] =>
)
)
)
)
)
)
)
[11] => xmlelement Object
(
[name] => onerror
[attributes] =>
[content] => Stop
[children] =>
)
)
)
)
)
[5] => xmlelement Object
(
[name] => button
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => name
[attributes] =>
[content] => Ok
[children] =>
)
[1] => xmlelement Object
(
[name] => value
[attributes] =>
[content] => send
[children] =>
)
[2] => xmlelement Object
(
[name] => action
[attributes] =>
[content] => aktion
[children] =>
)
[3] => xmlelement Object
(
[name] => bevorsave
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => function
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => php
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => include
[attributes] =>
[content] => file.php
[children] =>
)
)
)
)
)
)
)
[4] => xmlelement Object
(
[name] => aftersave
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => function
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => php
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => include
[attributes] =>
[content] => file.php
[children] =>
Nun mein schwieriges Problem. Da es sich um Objecte handelt, und ich mit sowas auf der Basis noch nicht gearbeitet habe, wie arbeite ich mit diesem Objekten, wie kann ich darauf zugreifen, Ebene für Ebene?
Ich danke euch.
LG Alex