I am new in PHP and I read article written by Dante Lorenso for
XML to Array conversion.
This is great article and very helpful for me. But I have problem using it.
SO,please, please help me.
I want to use this class and change the index to number like 0, 1, 2.... instead of NAME, DATA and _ELEMENTS.
so, i can use like this, $drive[0][0][1][0] = "somevalue"
Some how I got lots of problem converting this easy task.
So, please help me.
Here is class and sample code.
Thanks,
DevJan
I am trying to use the clas for XML to Array conversion from the Dante Lorenso's article.
This is great article and very helpful for me. But I have problem using it.
SO,please, please help me.
I want to use this class and change the index number to 0, 1, 2 like that instead of NAME, DATA and _ELEMENTS.
Some how I got lots of problem converting this easy task.
So, please help me.
Here is class and sample code.
Thanks,
DevJan
<?php
class XMLToArray
{
//-----------------------------------------
// private variables
var $parser;
var $node_stack = array();
var $counter =0;
var $c_c = array();
var $data = array();
var $d=0; // for data index
var $n=0; // for name tag index
var $a=0; // for array index
//var $startClock = time();
//-----------------------------------------
/ PUBLIC
If a string is passed in, parse it right away.
/
function XMLToArray($xmlstring="") {
if ($xmlstring) return($this->parse($xmlstring));
return(true);
}
//-----------------------------------------
/ PUBLIC
Parse a text string containing valid XML into a multidimensional array
located at rootnode.
/
function parse($xmlstring="") {
$startClock = time();
// set up a new XML parser to do all the work for us
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($this->parser, "startElement", "endElement");
xml_set_character_data_handler($this->parser, "characterData");
// Build a Root node and initialize the node_stack...
$this->node_stack = array();
$this->startElement(null, "root", array());
// parse the data and free the parser...
xml_parse($this->parser, $xmlstring);
xml_parser_free($this->parser);
// recover the root node from the node stack
$rnode = array_pop($this->node_stack);
// return the root node...
return($rnode);
}
//-----------------------------------------
/** PROTECTED
Start a new Element. This means we push the new element onto the stack
and reset it's properties.
/
function startElement($parser, $name, $attrs)
{
$this->d=0;
// create a new node...
$node = array();
$node[$this->n] = $name;
$this->n++;
foreach ($attrs as $key => $value) {
$node[$key] = $value;
/ Here is the attribute and it's value
echo "<br>key = $key";
echo"<br>node = $node[$key]";/
}
//$node[$this->d] = "";
$node[$this->a] = array();
$this->d++;
// add the new node to the end of the node stack
array_push($this->node_stack, $node);
//$this->counter++;
//$this->c_c[$this->counter] = $this->c_c[$this->counter] + 1;
}
//-----------------------------------------
/** PROTECTED
End an element. This is done by popping the last element from the
stack and adding it to the previous element on the stack.
*/
function endElement($parser, $name) {
// pop this element off the node stack
$node = array_pop($this->node_stack);
$node[$this->d] = trim($node[$this->d]);
//Here is the array of all nodes print('<pre>');print_r($node);print('</pre>');
// and add it an an element of the last node in the stack...
$lastnode = count($this->node_stack);
array_push($this->node_stack[$lastnode-1][$this->a], $node);
//$this->counter--;
//$this->c_c[$this->counter] = $this->c_c[$this->counter] - 1;
}
//-----------------------------------------
/** PROTECTED
Collect the data onto the end of the current chars.
/
function characterData($parser, $data) {
// add this data to the last node in the stack...
$lastnode = count($this->node_stack);
$this->node_stack[$lastnode-1][$this->d] .= $data;
// Here is the only data echo "<br>$data";
}
//-----------------------------------------
}
?>
<?
require_once("XMLToArray.php");
$xml2a = new XMLToArray();
//$xml_text="<me><you>Hello</you><he>Hi</he></me>";
$xml_text="filename.xml";
if(is_file($xml_text)){
if(!($fp = fopen($xml_text, "r"))){
die("IO Error $xml_text");
}
$xml_text = "";
while($chunk = fread($fp,4096)){
//$xml_text = $chunk;
$xml_text .= $chunk;
}
// $xml_data = file_get_contents($xml);
}
else{
$xml_text = $xml_text;
}
//print $xml_text;
//$xml_text="filename.xml";
$j=0;
//put at end of page before /body and /h // tml tags
$root_node=$xml2a->parse($xml_text);
print('<pre>'); print_r($drive); print('</pre>');
?>