I had the same problem a week ago, so i wrote a little function to
convert xml files into usable objects (the dom_xmltree function sucks a
lot),
Basicly it converts the XML data into a object and make arrays for multi
nodes, i also have done another function to write the object on disk,
may be very useful for your kind of application.
Be sure to have DOMXML installed on your system!
Use
$obj = load_xml($file);
write_xml($obj,$file);
$raiz = $null;
$larray = $null;
function doit($node) {
global $raiz;
global $larray;
if($node->has_child_nodes() ) {
$name = $node->tagname;
if ( $raiz == $null ) {
$raiz = $name;
}
$nodes = $node->children();
while($child = array_shift($nodes)) {
if(($child->type == XML_TEXT_NODE) &&
!($child->is_blank_node()) ) {
$name = $node->tagname;
$value = $child->content;
$obj = $value;
}
else if (($child->type == XML_ELEMENT_NODE)) {
$name = $child->tagname;
$pai = $child->parent_node();
if ( $pai->tagname == $raiz ) {
if ( ($checkarray) &&
(in_array($name,$checkarray)) ) {
if ( $name != $larray ) { $new =
$null ; }
if ( ! $new ) {
$new[] =
$obj->$raiz->$name ;
$larray = $name;
}
$new[] = doit($child);
$obj->$raiz->$name = $new;
} else {
$obj->$raiz->$name = doit($child);
}
} else {
if ( ($checkarray) &&
(in_array($name,$checkarray)) ) {
if ( $name != $larray ) { $new =
$null ; }
if ( ! $new ) {
$new[] =
$obj->$name;
$larray = $name;
}
$new[] = doit($child);
$obj->$name = $new;
}
else {
$obj->$name = doit($child);
}
}
$checkarray[] = $child->tagname;
}
}
}
return($obj);
}
// Entrada: path do xml
// Saida: array com o conteudo do xml
function load_xml($file) {
if(!$dom = domxml_open_file($file)) {
echo "Error while parsing the document\n";
exit;
}
$root = $dom->document_element();
$obj = doit($root);
return($obj);
}
$wdom = $null;
function get_vars($obj,$father) {
global $wdom;
$vars = get_object_vars($obj);
$keys = array_keys($vars);
for($i=0; $keys[$i] ; $i++ ) {
if ( is_object($obj->$keys[$i])) {
$element = $wdom->create_element($keys[$i]);
if ( ! $father ) {
$wdom->append_child($element);
} else {
$father->append_child($element);
}
get_vars($obj->$keys[$i],$element);
} else {
if ( ! is_array($obj->$keys[$i]) ) {
$child = $wdom->create_element($keys[$i]);
$data = $wdom->create_text_node($obj->$keys[$i]);
$child->add_child($data);
$father->add_child($child);
} else {
$ar = $keys[$i];
while( list($k,$v) = each ($obj->$ar) ) {
$child =
$wdom->create_element($keys[$i]);
if ( is_object($v) ) {
get_vars($v,$child);
} else {
$data =
$wdom->create_text_node($v);
$child->add_child($data);
}
$father->add_child($child);
}
}
}
}
}
function write_xml($obj,$file) {
global $wdom;
$wdom = domxml_new_doc('1.0');
get_vars($obj,$null);
$wdom->dump_file($file,false,true);
}
-----Mensagem original-----
De: Burhan Khalid [mailto:[EMAIL PROTECTED]
Enviada em: quinta-feira, 20 de mar�o de 2003 21:22
Para: [EMAIL PROTECTED]
Assunto: [PHP] XML Problems
Hello List,
Having some serious headaches trying to fix XML integration into
a PHP class. Am new to XML, but have been comfortable with PHP
for quite a while so please reply accordingly.
Here is the problem :
* Data handing functions are being called multiple times
* Randomly selected data is being stored in the class
Output of the class : http://www.meidomus.com/weather/xml.php
The XML file its parsing :
http://weather.interceptvector.com/weather.xml?id=QkFYWDAwMDE%3D
Class source : http://www.meidomus.com/weather/xml.phps
Thanks for any pointers :)
--
Best regards,
Burhan
mailto:[EMAIL PROTECTED]
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php