Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : XML Parser Functions : xml_parse

xml_parse

Start parsing an XML document (PHP 4, PHP 5)
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.

Examples ( Source code ) xml_parse

<?php
$file 
"contact.xml";
   
function 
startElement($parser$name$attrs) {
    print 
"<B>$name =></B>  ";
}

function 
endElement($parser$name) {
    print 
"n";
}
   
function 
characterData($parser$value) {
    print 
"$value<BR>";
}
   
$simpleparser xml_parser_create();
xml_set_element_handler($simpleparser"startElement""endElement");
xml_set_character_data_handler($simpleparser"characterData");
   
if (!(
$fp fopen($file"r"))) {
  die(
"could not open XML input");
}
   
while(
$data fread($fpfilesize($file))) {
  if (!
xml_parse($simpleparser$datafeof($fp))) {
     die(
xml_error_string(xml_get_error_code($simpleparser)));
  }
}

xml_parser_free($simpleparser);
?>
<!--
<contact id="43956">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National Company</employer>
          <dob>1951-02-02</dob>
     </personal>
</contact>

-->

Related Examples ( Source code ) » xml_parse











Code Examples / Notes » xml_parse

edvardas

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));
?>


eewon

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

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
}
?>


php

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 "

" . $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;
     }
   }
 }
}
?>


dgrimes

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.


byk

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 )
{
array_pop( $this->pointer );
$this->index--;
}
 
function _cData( $parser, $data )
{
if (empty($this->pointer[$this->index])) {
if (rtrim($data, "\n"))
$this->pointer[$this->index] = $data;
} else {
$this->pointer[$this->index] .= $data;
}
}
function _cleanString( $string )
{
return utf8_decode( trim( $string ) );
}
}
$m_xml = new CXml();
$xml_data = file_get_contents( $filename );
$m_xml->Set_XML_data( $xml_data );
$newsid = $m_xml->obj_data->root[0]->NewsID[0];


kyle bresin

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) != '') {


bbellwfu

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>


michelek

its maybe not better, but me thinks its more stright-forward
--INPUT:
<?xml version="1.0" encoding="UTF-8"?>
<world>
<country name="sweden">
<city name="stockholm">
<user>Adam</user>
<user>Eva</user>
</city>
<city name="göteborg">
<user>God</user>
</city>
</country>
<country name="usa">
<city name="new york">
<user>Clinton</user>
<user>Bush</user>
</city>
</country>
</world>
--CODE:
<?
/**
* m_i_h_k_e_l_AT_w_w_DOT_e_e
* 26.10.2003
**/
$filename = "m.m.xml";
$xmlC = new XmlC();
$xml_data = file_get_contents( $filename );
$xmlC->Set_XML_data( $xml_data );
echo( "<pre>\n" );
print_r( $xmlC->obj_data );
echo( "</pre>\n" );
class XmlC
{
 var $xml_data;
 var $obj_data;
 var $pointer;
 function XmlC()
 {
 }
 function Set_xml_data( &$xml_data )
 {
   $this->index = 0;
   $this->pointer[] = &$this->obj_data;
   $this->xml_data = $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;
   }
   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 )
 {
   array_pop( $this->pointer );
   $this->index--;
 }
 function _cData( $parser, $data )
 {
   if( trim( $data ) )
   {
     $this->pointer[$this->index] = trim( $data );
   }
 }
 function _cleanString( $string )
 {
   return utf8_decode( trim( $string ) );
 }
}
?>


tgrabietz

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");
?>


06-dec-2001 08:14

if you're using magic quotes by default, remember to turn them off for the XML parsing.

adam tylmad

I've created a parser that returns an
object based on a xml document.
example:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<country name="sweden">
   <city name="stockholm">
       <user>Adam</user>
       <user>Eve</user>
   </city>
   <city name="göteborg">
       <user>God</user>
   </city>
</country>
<country name="usa">
   <city name="new york">
       <user>Clinton</user>
       <user>Bush</user>
   </city>
</country>
generates the following object structure:
[country] => Array
(
   [0] => stdClass Object
       (
           [name] => sweden
           [city] => Array
               (
                   [0] => stdClass Object
                       (
                           [name] => stockholm
                           [user] => Array
                               (
                                   [0] => Adam
                                   [1] => Eve
                               )
                       )
                   [1] => stdClass Object
                       (
                           [name] => göteborg
                           [user] => God
                       )
               )
       )
   [1] => stdClass Object
       (
           [name] => usa
           [city] => stdClass
               (
                   [name] => new york
                   [user] => Array
                       (
                           [0] => Clinton
                           [1] => Bush
                       )
               )
       )
)
Here is the code:
class XMLParser {
var $path;
var $result;
function XMLParser($encoding, $data) {
$this->path = "\$this->result";
$this->index = 0;

$xml_parser = xml_parser_create($encoding);
xml_set_object($xml_parser, &$this);
xml_set_element_handler($xml_parser, 'startElement', 'endElement');
xml_set_character_data_handler($xml_parser, 'characterData');
xml_parse($xml_parser, $data, true);
xml_parser_free($xml_parser);
}

       function startElement($parser, $tag, $attributeList) {
       eval("\$vars = get_object_vars(".$this->path.");");
        $this->path .= "->".$tag;
       if ($vars and array_key_exists($tag, $vars)) {
            eval("\$data = ".$this->path.";");
                if (is_array($data)) {
                          $index = sizeof($data);
                          $this->path .= "[".$index."]";
                    } else if (is_object($data)) {
                          eval($this->path." = array(".$this->path.");");
                          $this->path .= "[1]";
                    }
        }
       eval($this->path." = null;");
       foreach($attributeList as $name => $value)
           eval($this->path."->".$name. " = '".XMLParser::cleanString($value)."';");
   }

function endElement($parser, $tag) {
$this->path = substr($this->path, 0, strrpos($this->path, "->"));
}

function characterData($parser, $data) {
eval($this->path." = '".trim($data)."';");
}
}
enjoy! And please make it better if you can ;-)


tim

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)


talraith

I have written a module that contains a class for use with XML documents.  The module is dual-purpose in that it will parse XML code into a native object tree structure and will generate XML code from the object tree structure.
The output produced from generating XML code is designed to be used by other applications and is not in human-readable form.
I would like to point out that the code does not use any eval() statements to create the tree.
I am posting my code for two purposes:
1) I am looking to refine it and make it more efficient
2) It may benefit someone that is looking for a module like this.
The code is too big to post in here, so I have uploaded it to a web site:  http://www.withouthonor.com/obj_xml.html
An example of parsing an XML document:
<?php
// Use the method of your choice to load the XML document
// into a variable...  (a very generic sample follows)
$XML = '<root><section>This is my sample XML code</section></root>';
$xml = new xml_doc($XML);
$xml->parse();
// Access root level object
$my_tag = $xml->getTag(0,$name,$attributes,$cdata,$children);
?>
It is then possible to loop through the children of the tag and process the data with your program.  The last variable above ($children) contains a list of tag reference ID's.  The object tree is created by assigning each tag a unique ID starting with zero.  The tree is created by using object references to relate parents and children.
If you wanted to create an XML document from scratch, the code would be similar to the following example:
<?php
$xml = new xml_doc();
// Create the root tag and retrieve reference ID #
$root_tag = $xml->createTag('root');
// The second parameter is for tag attributes
// This is done using an associative array
$xml->createTag('section',array(),'This is my sample XML code',$root_tag);
$my_output = $xml->generate();
print $my_output;
?>
The example above creates an XML document that is the same as the one used in my first example.  Another option available would be to load the XML code as in the first example, change it through PHP, and then generate the code and output it.


james @at@ mercstudio dot com dot nospam

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();


randlem

Here's a handy way to generate a tree that can be can be decended easily.
<?php
$file = 'xmltest.xml';
$tag_tree = array();
$stack = array();
class tag {
var $name;
var $attrs;
var $children;
function tag($name, $attrs, $children) {
$this->name = $name;
$this->attrs = $attrs;
$this->children = $children;
}
}
function startTag($parser, $name, $attrs) {
global $tag_tree, $stack;
$tag = new tag($name,$attrs,'');
array_push($stack,$tag);
}
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");
$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("\n");
print_r($stack);
print("\n");
?>


alex dot garcia

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;
}


jacek prucia 7bulls com

don't underestimate is_final argument. If you ignore it (since it is optional) you can get strange results with non well-formed XML's, like no output from xml_parse at all. Also if you use feof($fp) as is_final make sure you don't use fgets, because there's a caveat with how feof is evaluated there.

ben

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...


Change Language


Follow Navioo On Twitter
utf8_decode
utf8_encode
xml_error_string
xml_get_current_byte_index
xml_get_current_column_number
xml_get_current_line_number
xml_get_error_code
xml_parse_into_struct
xml_parse
xml_parser_create_ns
xml_parser_create
xml_parser_free
xml_parser_get_option
xml_parser_set_option
xml_set_character_data_handler
xml_set_default_handler
xml_set_element_handler
xml_set_end_namespace_decl_handler
xml_set_external_entity_ref_handler
xml_set_notation_decl_handler
xml_set_object
xml_set_processing_instruction_handler
xml_set_start_namespace_decl_handler
xml_set_unparsed_entity_decl_handler
eXTReMe Tracker