PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

xml_parser_create_ns> <xml_parse_into_struct
Last updated: Fri, 10 Oct 2008

view this page in

xml_parse

(PHP 4, PHP 5)

xml_parseStart parsing an XML document

Description

int xml_parse ( resource $parser , string $data [, bool $is_final ] )

xml_parse() parses an XML document. The handlers for the configured events are called as many times as necessary.

Parameters

parser

A reference to the XML parser to use.

data

Chunk of data to parse. A document may be parsed piece-wise by calling xml_parse() several times with new data, as long as the is_final parameter is set and TRUE when the last data is parsed.

is_final

If set and TRUE, data is the last piece of data sent in this parse.

Return Values

Returns 1 on success or 0 on failure.

For unsuccessful parses, error information can be retrieved with xml_get_error_code(), xml_error_string(), xml_get_current_line_number(), xml_get_current_column_number() and xml_get_current_byte_index().

Note: Entity errors are reported at the end of the data thus only if is_final is set and TRUE.



xml_parser_create_ns> <xml_parse_into_struct
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
xml_parse
hello at rootsy dot co dot uk
24-May-2008 05:30
This is a follow up to the parser class posted by neoyahuu at yahoo dot com. The xml_set_character_data_handler function falls prey to the weird splitting caused by special characters (i.e. new lines whenever an umlaut is found) - my fix just uses concatenation to stop this from happening. This is a great function otherwise. The code:

<?php

class xx_xml {

   
// XML parser variables
   
var $parser;
    var
$name;
    var
$attr;
    var
$data  = array();
    var
$stack = array();
    var
$keys;
    var
$path;
  
   
// either you pass url atau contents.
    // Use 'url' or 'contents' for the parameter
   
var $type;

   
// function with the default parameter value
   
function xx_xml($url='http://www.opocot.com', $type='url') {
       
$this->type = $type;
       
$this->url  = $url;
       
$this->parse();
    }
  
   
// parse XML data
   
function parse()
    {
       
$data = '';
       
$this->parser = xml_parser_create ("UTF-8");
       
xml_set_object($this->parser, $this);
       
xml_set_element_handler($this->parser, 'startXML', 'endXML');
       
xml_set_character_data_handler($this->parser, 'charXML');

       
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);

        if (
$this->type == 'url') {
           
// if use type = 'url' now we open the XML with fopen
          
           
if (!($fp = @fopen($this->url, 'rb'))) {
               
$this->error("Cannot open {$this->url}");
            }

            while ((
$data = fread($fp, 8192))) {
                if (!
xml_parse($this->parser, $data, feof($fp))) {
                   
$this->error(sprintf('XML error at line %d column %d',
                   
xml_get_current_line_number($this->parser),
                   
xml_get_current_column_number($this->parser)));
                }
            }
        } else if (
$this->type == 'contents') {
           
// Now we can pass the contents, maybe if you want
            // to use CURL, SOCK or other method.
           
$lines = explode("\n",$this->url);
            foreach (
$lines as $val) {
                if (
trim($val) == '')
                    continue;
               
$data = $val . "\n";
                if (!
xml_parse($this->parser, $data)) {
                    echo
$data.'<br />';
                   
$this->error(sprintf('XML error at line %d column %d',
                   
xml_get_current_line_number($this->parser),
                   
xml_get_current_column_number($this->parser)));
                }
            }
        }
    }

    function
startXML($parser, $name, $attr)    {
       
$this->stack[$name] = array();
       
$keys = '';
       
$total = count($this->stack)-1;
       
$i=0;
        foreach (
$this->stack as $key => $val)    {
            if (
count($this->stack) > 1) {
                if (
$total == $i)
                   
$keys .= $key;
                else
                   
$keys .= $key . '|'; // The saparator
           
}
            else
               
$keys .= $key;
           
$i++;
        }
        if (
array_key_exists($keys, $this->data))    {
           
$this->data[$keys][] = $attr;
        }    else
           
$this->data[$keys] = $attr;
       
$this->keys = $keys;
    }

    function
endXML($parser, $name)    {
       
end($this->stack);
        if (
key($this->stack) == $name)
           
array_pop($this->stack);
    }

    function
charXML($parser, $data)    {
        if (
trim($data) != '')
            @
$startFrom = count($this->data[$this->keys])-1; // fixes weird splitting (bug?)
           
@$startFrom = $startFrom == -1 ? $startFrom = 0 : $startFrom;
            @
$this->data[$this->keys]['data'][$startFrom] .= trim(str_replace("\n", '', $data));
    }

    function
error($msg)    {
        echo
"<div align=\"center\">
            <font color=\"red\"><b>Error: $msg</b></font>
            </div>"
;
        exit();
    }
}

?>
Frank
22-Feb-2008 12:09
And with that, why not use recursivity to get to all the CDATA...

<?php

function printCDATA($pItem)
{
  foreach(
$pItem as $value){
    if (isset(
$value[children])){
     
printCDATA($value[children]);
    }else{
      if(isset(
$value[name])){
        echo
$value[name].": ";
      }
      if(isset(
$value[cdata])){
        echo
$value[cdata]."<br>";
      }
    }
  } 
}

printCDATA($stack);
?>
neoyahuu at yahoo dot com
12-Dec-2007 11:18
Instead of passing a URL, we can pass the XML content to this class (either you
 want to use CURL, Socks or fopen to retrieve it first) and instead of using
 array, I'm using separator '|' to identify which data to get (in order to make
 it short to retrieve a complex XML data). Here is my class with built-in fopen
 which you can pass URL or you can pass the content instead :

p/s : thanks to this great help page.

<?php

class xx_xml {

   
// XML parser variables
   
var $parser;
    var
$name;
    var
$attr;
    var
$data  = array();
    var
$stack = array();
    var
$keys;
    var
$path;
   
   
// either you pass url atau contents.
    // Use 'url' or 'contents' for the parameter
   
var $type;

   
// function with the default parameter value
   
function xx_xml($url='http://www.opocot.com', $type='url') {
       
$this->type = $type;
       
$this->url  = $url;
       
$this->parse();
    }
   
   
// parse XML data
   
function parse()
    {
       
$data = '';
       
$this->parser = xml_parser_create();
       
xml_set_object($this->parser, $this);
       
xml_set_element_handler($this->parser, 'startXML', 'endXML');
       
xml_set_character_data_handler($this->parser, 'charXML');

       
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);

        if (
$this->type == 'url') {
           
// if use type = 'url' now we open the XML with fopen
           
           
if (!($fp = @fopen($this->url, 'rb'))) {
               
$this->error("Cannot open {$this->url}");
            }

            while ((
$data = fread($fp, 8192))) {
                if (!
xml_parse($this->parser, $data, feof($fp))) {
                   
$this->error(sprintf('XML error at line %d column %d',
                   
xml_get_current_line_number($this->parser),
                   
xml_get_current_column_number($this->parser)));
                }
            }
        } else if (
$this->type == 'contents') {
           
// Now we can pass the contents, maybe if you want
            // to use CURL, SOCK or other method.
           
$lines = explode("\n",$this->url);
            foreach (
$lines as $val) {
                if (
trim($val) == '')
                    continue;
               
$data = $val . "\n";
                if (!
xml_parse($this->parser, $data)) {
                   
$this->error(sprintf('XML error at line %d column %d',
                   
xml_get_current_line_number($this->parser),
                   
xml_get_current_column_number($this->parser)));
                }
            }
        }
    }

    function
startXML($parser, $name, $attr)    {
       
$this->stack[$name] = array();
       
$keys = '';
       
$total = count($this->stack)-1;
       
$i=0;
        foreach (
$this->stack as $key => $val)    {
            if (
count($this->stack) > 1) {
                if (
$total == $i)
                   
$keys .= $key;
                else
                   
$keys .= $key . '|'; // The saparator
           
}
            else
               
$keys .= $key;
           
$i++;
        }
        if (
array_key_exists($keys, $this->data))    {
           
$this->data[$keys][] = $attr;
        }    else
           
$this->data[$keys] = $attr;
       
$this->keys = $keys;
    }

    function
endXML($parser, $name)    {
       
end($this->stack);
        if (
key($this->stack) == $name)
           
array_pop($this->stack);
    }

    function
charXML($parser, $data)    {
        if (
trim($data) != '')
           
$this->data[$this->keys]['data'][] = trim(str_replace("\n", '', $data));
    }

    function
error($msg)    {
        echo
"<div align=\"center\">
            <font color=\"red\"><b>Error: $msg</b></font>
            </div>"
;
        exit();
    }
}

?>

And example of retrieving XML data:
p/s: example use to retrieve weather

<?php
include_once "xx_xml.class.php";

// Im using simple curl (the original is in class) to get the contents

$pageurl = "http://xml.weather.yahoo.com/forecastrss?p=MYXX0008&u=c";
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $pageurl );
$thecontents = curl_exec ( $ch );
curl_close($ch);

// We want to pass only a ready XML content instead of URL
// But if you want to use URL , skip the curl functions above and use this
// $xx4 = new xx_xml("url here",'url');

$xx4 = new xx_xml($thecontents,'contents');
// As you can see, we use saparator '|' instead of long array
$Code = $xx4->data ['rss|channel|item|yweather:condition']['code'] ;
$Celcius = $xx4->data ['rss|channel|item|yweather:condition']['temp'] ;
$Text = $xx4->data ['rss|channel|item|yweather:condition']['text'] ;
$Cityname = $xx4->data ['rss|channel|yweather:location']['city'] ;

?>

Hope this helps.
eewon at NOSPAMnowee dot org
20-Oct-2007 12:21
Whilst implementing my RSS_FEED reader, I stumbled upon a slight issue, typically on RSS feeds such as the NY Times ones : quotes and apostrophes were replaced by question tags. For example : "the creator?s" instead of "the creator's" due to the use of &#8217;

for that reason I used the following simple trick to make sure all these HTML entities would be converted correctly :

function code2code($data)
{
    $trans = array(
      '&#8216;'=>'&#145;',
      '&#8217;'=>'&#146;',
      '&#8220;'=>'&#147;',
      '&#8221;'=>'&#148;',
      '&#8226;'=>'&#149;',
      '&#8211;'=>'&#150;',
      '&#8212;'=>'&#151;');
    return strtr($data, $trans);
}

while($data=fread($fp,4096))
{
    xml_parse($xml_parser,code2code($data),feof($fp));
}
fclose($fp);

I'm sure it can be done better, and that it lacks some other entries, yet it works for me.
joris dot landman at chello dot nl
28-Jul-2007 10:42
This page has been a great help! I've adapted the examples below to make a class to parse an X(HT)ML file to a multidimensional array.

Spaces, tabs, breaks, etc. are included in the array as TEXT_NODE, since in XHTML they may be functional. A function to trim them can easily be added if so desired.

I've written my class to store multiple tags with the same name. In order to do this I've used nested arrays with the key NODES to store the order in which tags and data were parsed. These NODES arrays can be used as a blueprint to reconstruct the X(HT)ML part of the document in it's entirity, including formatting. (Doctype will have to be added for validity).

<?php

class my_xml_object {

    var
$xml_data; # raw xml data from file
   
function parse_xml_file($my_uri) {
       
$this->xml_data = null; # clear previously parsed file and related variables
       
if (is_file($my_uri) && is_readable($my_uri)) { # existing and readable uri?
           
$my_file = fopen($my_uri, "r");
            while(
$my_xml_input = fread($my_file, filesize($my_uri))) {
               
$this->xml_data .= $my_xml_input; # add data to xml_data
           
}
           
fclose($my_file);
           
$this->parse_xml_data($this->xml_data); # parse data
       
} else {
           
trigger_error("supplied argument is not a URI to a (readable) file", E_USER_ERROR);
        }
    }

    var
$xml_array = Array(); # xml array from parsed data
   
function parse_xml_data($my_data) { # adapted from class by randlem@gmail.com, tgrabietz@bupnet.de, bbellwfu@gmail.com, Kyle Bresin - see http://nl2.php.net/xml_parse
       
$this->xml_array = Array(0 => Array()); # clear previously parsed file and related variables; populate first element
       
$my_parser = xml_parser_create(); # set up parser
       
xml_set_object($my_parser, $this); # enable parser within object
       
xml_set_element_handler($my_parser, "xml_tag_open", "xml_tag_close");
       
xml_set_character_data_handler($my_parser, "xml_tag_data");
        if (!
xml_parse($my_parser, $my_data)) {
           
trigger_error("data can not be parsed", E_USER_ERROR); # inspect problems #die(sprintf("<br />\n<b>Error</b>:  %s on line <b>%d</b><br />\n", xml_error_string(xml_get_error_code($my_parser)), xml_get_current_line_number($my_parser)));
       
}
       
xml_parser_free($my_parser); # free parser
       
return $this->xml_array; # return xml array
   
}

    var
$my_branch = Array();
    function
xml_tag_open($my_parser, $my_name, $my_attributes) {
       
array_push($this->my_branch, $my_name); # add tag name to branch
       
$this->xml_array[] = Array(); # nest array in xml array for data
       
if (count($my_attributes)) {
           
$this->xml_array[count($this->xml_array) - 1]["ATTRIBUTES"] = $my_attributes; # nest attributes array
       
}
    }

    function
xml_tag_data($my_parser, $my_data) {
       
$this->xml_array[count($this->my_branch)]["TEXT_NODE"][] = $my_data; # add data to nested array
       
$this->xml_array[count($this->my_branch)]["NODES"][] = "TEXT_NODE"; # add text node to nested NODES array
   
}

    function
xml_tag_close($my_parser, $my_name) {
       
$this->xml_array[count($this->my_branch) - 1][$this->my_branch[count($this->my_branch) - 1]][] = $this->xml_array[count($this->xml_array) - 1]; # nest arrays to follow document structure
       
array_pop($this->xml_array); # pop off element that was nested
       
$this->xml_array[count($this->my_branch) - 1]["NODES"][] = $my_name; # add tag node to nested NODES array
       
array_pop($this->my_branch); # update branch
   
}

?>
dgrimes at scvl dot com
29-Jun-2007 09:14
One note about magic quotes: magic_quotes_runtime needs to be disabled before parsing XML. It can cause strange errors during parsing. Just add the following at the top of your program:

set_magic_quotes_runtime(0);

If you need magic quotes you can use stripslashes or save the current magic quotes setting with get_magic_quotes_runtime() then disable and parse your XML and then restore the previous magic quotes setting.
php at b635 dot com
23-Jun-2007 01:17
The suggestions below have been a great help, but there was one thing I really needed...

I'm parsing Amazon XML data, and I wanted to be able to index into the array using something like:

<?php
 
print "<p>" . $strAXML->arrOutput[ITEMLOOKUPRESPONSE][ITEMS][ITEM][SMALLIMAGE][URL];
?>

To solve this, I had to push all of the open tags onto a separate stack, add any tag data to the tail end of an attributed multi-dimensional array, and then pop the tag name off of the stack once it was closed...

<?php

class xml2array {
 
  var
$arrOutput = array();
  var
$arrName = array();
  var
$objParser;
  var
$strXmlData;
 
  function
parse($strInputXML) {

   
// standard XML parse object setup
 
   
$this->objParser = xml_parser_create ();
   
xml_set_object($this->objParser,$this);
   
xml_set_element_handler($this->objParser, "tagOpen", "tagClosed");
   
   
xml_set_character_data_handler($this->objParser, "tagData");
 
   
$this->strXmlData = xml_parse($this->objParser,$strInputXML );
    if(!
$this->strXmlData) {
      die(
sprintf("XML error: %s at line %d",
       
xml_error_string(xml_get_error_code($this->objParser)),
       
xml_get_current_line_number($this->objParser)));
    }
       
   
xml_parser_free($this->objParser);
   
    return
$this->arrOutput;
  }

  function
tagOpen($parser, $name, $attrs) {
   
// push the current tag name to an array of still-open tag names
   
array_push ($this->arrName, $name);

   
// merge the array of current attributes to the open tag
    // NOTE: this does not currently handle multiple attributes with the same name
    // (i.e. it will overwrite them with the last values)

   
$strEval = "\$this->arrOutput";
    foreach (
$this->arrName as $value) {
     
$strEval .= "[" . $value . "]";
    }
   
$strEval = $strEval . " = array_merge (" . $strEval . ",\$attrs);";

    eval (
$strEval);
  }

  function
tagData($parser, $tagData) {  

   
// set the latest open tag equal to the tag data

   
$strEval = "\$this->arrOutput";
    foreach (
$this->arrName as $value) {
     
$strEval .= "[" . $value . "]";
    }

   
$strEval = $strEval . " = \$tagData;";

    eval (
$strEval);
  }

 
  function
tagClosed($parser, $name) {

   
// pop this tag (and any subsequent tags) off the stack of open tag names

   
for ($i = count ($this->arrName) - 1; $i > 0; $i--) {
     
$currName = $this->arrName[$i];
     
array_pop ($this->arrName);
      if (
$currName == $name) {
        break;
      }
    }
  }

}

?>
edvardas at alderonas dot lt
18-Aug-2006 02:38
xml_parse() crashes when xml file contains chars \x00 - \x1f, so be careful! I solve this problem simple:
<?php
$bad_chr
= array("\x00" => "chr(0)", "\x01" => "chr(1)", "\x02" => "chr(2)", "\x03" => "chr(3)", "\x04" => "chr(4)", "\x05" => "chr(5)", "\x06" => "chr(6)", "\x07" => "chr(7)", "\x08" => "chr(8)", "\x09" => "chr(9)", "\x0a" => "chr(10)", "\x0b" => "chr(11)", "\x0c" => "chr(12)", "\x0d" => "chr(13)", "\x0e" => "chr(14)", "\x0f" => "chr(15)", "\x10" => "chr(16)", "\x11" => "chr(17)", "\x12" => "chr(18)", "\x13" => "chr(19)", "\x14" => "chr(20)", "\x15" => "chr(21)", "\x16" => "chr(22)", "\x17" => "chr(23)", "\x18" => "chr(24)", "\x19" => "chr(25)", "\x1a" => "chr(26)", "\x1b" => "chr(27)", "\x1c" => "chr(28)", "\x1d" => "chr(29)", "\x1e" => "chr(30)", "\x1f" => "chr(31)");
xml_parse($xml_parser, strtr($data, $bad_chr), feof($fp));
//....
$parsed_data = strtr($parsed_data, array_flip($bad_chr));
?>
james @at@ mercstudio dot Com dot nospam
02-Apr-2006 03:10
hi,

i've modified bbellwfu at gmail dot com to as below:

features added:
  - toXML (convert back array to xml string)
  - changed name, according to macromedia flash xml concept : children -> childrens, tagdata -> nodevalue, name -> nodename,
  - added pointer firstchild to childrens[0] (if exists)

some findings that i would like to share:
 - <![cdata[my value here]]> (does not work on property value
 - xml file must be htmlentity based (if not using cdata)
 - xml line feed on node data seems to be double line feed on windows (still figuring why)
 - xml line feed on attribute value seems to be ignored...

here's my code below :)

class u007xml
{
   var $arrOutput = array();
   var $resParser;
   var $strXmlData;
  
  
    function u007xml($tfile = "")
    {
        if(trim($tfile) != "") { $this->loadFile($tfile);}
    }
   
    function loadFile($tfile)
    {
        $this->thefile = $tfile;
       
        $th = file($tfile);
        $tdata = implode("\n", $th);
       
        return $this->parse($tdata);
    }
   
   function parse($strInputXML)
   {
        $this->resParser = xml_parser_create ();
        xml_set_object($this->resParser,$this);
        xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
       
        xml_set_character_data_handler($this->resParser, "tagData");
       
        $this->strXmlData = xml_parse($this->resParser,$strInputXML );
       
        if(!$this->strXmlData) {
           die(sprintf("XML error: %s at line %d",
        xml_error_string(xml_get_error_code($this->resParser)),
        xml_get_current_line_number($this->resParser)));
        }
       
        xml_parser_free($this->resParser);
       
        return $this->arrOutput;
   }
  
   //called on each xml tree
   function tagOpen($parser, $name, $attrs) {
       $tag=array("nodename"=>$name,"attributes"=>$attrs);
       array_push($this->arrOutput,$tag);
   }
 
  //called on data for xml
   function tagData($parser, $tagData) {
       if(trim($tagData)) {
           if(isset($this->arrOutput[count($this->arrOutput)-1]['nodevalue'])) {
               $this->arrOutput[count($this->arrOutput)-1]['nodevalue'] .= $this->parseXMLValue($tagData);
           }
           else {
               $this->arrOutput[count($this->arrOutput)-1]['nodevalue'] = $this->parseXMLValue($tagData);
           }
       }
   }
 
  //called when finished parsing
   function tagClosed($parser, $name) {
       $this->arrOutput[count($this->arrOutput)-2]['childrens'][] = $this->arrOutput[count($this->arrOutput)-1];
      
       if(count ($this->arrOutput[count($this->arrOutput)-2]['childrens'] ) == 1)
       {
            $this->arrOutput[count($this->arrOutput)-2]['firstchild'] =& $this->arrOutput[count($this->arrOutput)-2]['childrens'][0];
       }
       array_pop($this->arrOutput);
   }

    function toArray()
    {
        //not used, we can call loadString or loadFile instead...
    }
   
   
    function parseXMLValue($tvalue)
    {
        $tvalue = htmlentities($tvalue);
        return $tvalue;
    }
   
    function toXML($tob = null)
    {
        //return back xml
        $result = "";
       
        if( $tob == null)
        {
            $tob = $this->arrOutput;
        }
       
        if(!isset($tob))
        {
            echo "XML Array empty...";
            return null;
        }
       
       
        for($c = 0; $c < count($tob); $c++)
        {
            $result .="<" . $tob[$c]["nodename"];
           
            while (list($key, $value) = each($tob[$c]["attributes"]))
            {
                $result .=" " . $key."=\"" . $this->parseXMLValue($value) . "\"";
            }
           
            $result .= ">";
           
            //assign node value
            if( isset($tob[$c]["nodevalue"]) )
            {
                $result .= $tob[$c]["nodevalue"];
            }
           
            if( count($tob[$c]["childrens"]) > 0 )
            {
                $result .= "\r\n" . $this->toXML(&$tob[$c]["childrens"]) . "";
            }

            $result .= "</" . $tob[$c]["nodename"] . ">\r\n";
           
           
        }//end of each array...
       
        return $result;
    }
   
    function displayXML()
    {
        print_r($this->arrOutput);
    }
   
    function getXML($tob = null)
    {
        return "<?xml version='1.0'?>\r\n" . $this->toXML($tob);
    }

}//end of u007xml class

//examples below:

$xx = new u007xml();
$xx->loadFile("xml3.xml");

//$xx->displayXML();

print $xx->getXML();
ben at autonomic dot net
16-Mar-2006 08:12
bbellwfu's code does not handle 'text nodes' properly.
Consider the innards of a tag like <root>xxx<tag2/>yyy</root>
The 'tagData' for root will be "xxxyyy" and you have lost all information about where "tag2" was in that sequence.

Quick and dirty hack.

Replace tagData with this code :
   function tagData($parser, $tagData) {         
          $last_element=count($this->arrOutput)-1;         
        $this->arrOutput[$last_element]['children'][] = array("textnode",$tagData);      
   }

What this does is adds 'textnodes' as children of its containing parent, *in the right sequence* (rather like the internet browsers do it). This then lets you do some more sensible secondary work like recursively looking up internal references within the document...
Kyle Bresin
19-Jan-2006 06:15
Just wanted to note a small bug in bbellwfu's class (which is really great btw).

It fails to capture any datums which are equal to numerical zero.

The problem lies in the function tagData, the first if statement should be:

if(trim($tagData) != '') {
tim at alloutinteraction dot com
01-Oct-2005 10:29
I wanted to create a really simple XML parser, but I found the array management in xml_parse a bit daunting. So I flattened my XML and parsed it using string matching. It wouldn't be difficult to add xml depth (of 2 plus levels) by modifying the parsedXML array.

<?
// here's the raw html
$xmlRaw="<order>Order data</order><label>Label data</label><control>123</control>";
// here are the xml field names
$xmlFieldNames=array("order", "label", "control");
// for each xml field...
foreach ($xmlFieldNames as $xmlField) {
    if(
strpos($xmlRaw,$xmlField)!==false){
       
// I've broken 1 single line into 4 for display purposes
       
$parsedXML[$xmlField]=substr($xmlRaw,
       
strpos($xmlRaw,"<$xmlField>")+strlen("<$xmlField>"),
       
strpos($xmlRaw,"</$xmlField>")-strlen("<$xmlField>")
        -
strpos($xmlRaw,"<$xmlField>"));
    }
}
print_r($parsedXML);
// prints: Array ( [order] => Order data [label] => Label data [control] => 123 )

?>

Hope you find this useful (coded it while ill in bed with streaming cold, but felt much better afterwards!)

Tim (a lazy coder)
bbellwfu at gmail dot com
05-May-2005 05:51
Just improving a little bit on the code examples from tgrabietz and randlem below... everything in one pretty class, plus some checks in place so that the element data doesnt get split up (thanks to flobee on the xml_set_character_data_handler page)

<?php

/* Usage
 Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;

 $objXML = new xml2Array();
 $arrOutput = $objXML->parse($strYourXML);
 print_r($arrOutput); //print it out, or do whatever!
 
*/
class xml2Array {
   
    var
$arrOutput = array();
    var
$resParser;
    var
$strXmlData;
   
    function
parse($strInputXML) {
   
           
$this->resParser = xml_parser_create ();
           
xml_set_object($this->resParser,$this);
           
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
           
           
xml_set_character_data_handler($this->resParser, "tagData");
       
           
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
            if(!
$this->strXmlData) {
               die(
sprintf("XML error: %s at line %d",
           
xml_error_string(xml_get_error_code($this->resParser)),
           
xml_get_current_line_number($this->resParser)));
            }
                           
           
xml_parser_free($this->resParser);
           
            return
$this->arrOutput;
    }
    function
tagOpen($parser, $name, $attrs) {
      
$tag=array("name"=>$name,"attrs"=>$attrs);
      
array_push($this->arrOutput,$tag);
    }
   
    function
tagData($parser, $tagData) {
       if(
trim($tagData)) {
            if(isset(
$this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
               
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData;
            }
            else {
               
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
            }
       }
    }
   
    function
tagClosed($parser, $name) {
      
$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
      
array_pop($this->arrOutput);
    }
}
?>

Will output something like...

<snippet>
Array
(
    [0] => Array
        (
            [name] => GETMESSAGESRESPONSE
            [attrs] => Array
                (
                )

            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => STATUS
                            [attrs] => Array
                                (
                                )

                        )

</snippet>
alex dot garcia at noos dot fr
14-Mar-2005 04:47
Here is the inverse function which takes parsed xml array in entry and outputs xml string

enjoy !

function getXmlFromArray($root){
       
        if(count($root) > 0){

            $curr_name = $root['name'];
            $attribs = $root['attrs'];
            $curr_childs = $root['children'];
            $curr_data = $root['cdata'];
       
            $xml .= '<'.$curr_name;
           
            if(count($attribs) > 0){
                $i = 1;
                foreach($attribs as $key => $value){
                    $curr_attribs .= $key.'="'.$value.'"';
                    $i++;
                    if($i <= count($attribs)){
                        $curr_attribs .= ' ';
                    }
                }
                $xml .= ' '.$curr_attribs;
            }        
           
            if($curr_data != ''){
                $xml .= '><![CDATA['.$curr_data.']]></'.$curr_name.'>';
            } else {
                if(count($curr_childs) > 0){
                    $xml .= '>';
                    foreach($curr_childs as $child){
                        $xml .= getXmlFromArray($child);
                    }
                    $xml .= '</'.$curr_name.'>';
                } else {
                    $xml .= '/>';
                }   
            }
       
        }
        return $xml;
    }
tgrabietz at bupnet dot de
22-Sep-2004 11:05
it's like randlem at gmail dot com's great code, without using a "class container" but parsing cdata. The script returns the tree-structure in a single array.

<?php
$file
= 'simple.xml';
$stack = array();

function
startTag($parser, $name, $attrs)
{
   global
$stack;
  
$tag=array("name"=>$name,"attrs"=>$attrs);  
  
array_push($stack,$tag);
 
}

function
cdata($parser, $cdata)
{
    global
$stack,$i;
   
    if(
trim($cdata))
    {    
       
$stack[count($stack)-1]['cdata']=$cdata;   
    }
}

function
endTag($parser, $name)
{
   global
$stack;  
  
$stack[count($stack)-2]['children'][] = $stack[count($stack)-1];
  
array_pop($stack);
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "cdata");

$data = xml_parse($xml_parser,file_get_contents($file));
if(!
$data) {
   die(
sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}

xml_parser_free($xml_parser);

print(
"<pre>\n");
print_r($stack);
print(
"</pre>\n");
?>
ByK
16-Sep-2004 11:12
modified from yours code. I think it's work!!.
class CXml
{
    var $xml_data;
    var $obj_data;
    var $pointer;

    function CXml() { }
  
    function Set_xml_data( &$xml_data )
    {
        $this->index = 0;
        $this->pointer[] = &$this->obj_data;
   
        //strip white space between tags
        $this->xml_data = eregi_replace(">"."[[:space:]]+"."<","><",$xml_data);
        $this->xml_parser = xml_parser_create( "UTF-8" );
   
        xml_parser_set_option( $this->xml_parser, XML_OPTION_CASE_FOLDING, false );
        xml_set_object( $this->xml_parser, &$this );
        xml_set_element_handler( $this->xml_parser, "_startElement", "_endElement");
        xml_set_character_data_handler( $this->xml_parser, "_cData" );
      
        xml_parse( $this->xml_parser, $this->xml_data, true );
        xml_parser_free( $this->xml_parser );
    }
  
    function _startElement( $parser, $tag, $attributeList )
    {
        foreach( $attributeList as $name => $value )
        {
            $value = $this->_cleanString( $value );
            $object->$name = $value;
        }
        //replaces the special characters with the underscore (_) in tag name
        $tag = preg_replace("/[:\-\. ]/", "_", $tag);
        eval( "\$this->pointer[\$this->index]->" . $tag . "[] = \$object;" );
        eval( "\$size = sizeof( \$this->pointer[\$this->index]->" . $tag . " );" );
        eval( "\$this->pointer[] = &\$this->pointer[\$this->index]->" . $tag . "[\$size-1];" );
          
        $this->index++;
    }

    function _endElement( $parser, $tag )