|
 |
xml_parse_into_struct (PHP 3 >= 3.0.8, PHP 4, PHP 5) xml_parse_into_struct -- Parse XML data into an array structure Descriptionint xml_parse_into_struct ( resource parser, string data, array &values [, array &index] )
This function parses an XML file into 2 parallel array
structures, one (index) containing pointers
to the location of the appropriate values in the
values array. These last two parameters
must be passed by reference.
Замечание:
xml_parse_into_struct() returns 0 for
failure and 1 for success. This is not the same as FALSE
and TRUE, be careful with operators such as ===.
Below is an example that illustrates the internal structure of
the arrays being generated by the function. We use a simple
note tag embedded inside a
para tag, and then we parse this and print out
the structures generated:
Пример 1. xml_parse_into_struct() example
<?php
$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
?>
|
When we run that code, the output will be:
Index array
Array
(
[PARA] => Array
(
[0] => 0
[1] => 2
)
[NOTE] => Array
(
[0] => 1
)
)
Vals array
Array
(
[0] => Array
(
[tag] => PARA
[type] => open
[level] => 1
)
[1] => Array
(
[tag] => NOTE
[type] => complete
[level] => 2
[value] => simple note
)
[2] => Array
(
[tag] => PARA
[type] => close
[level] => 1
)
) |
|
Event-driven parsing (based on the expat library) can get
complicated when you have an XML document that is complex.
This function does not produce a DOM style object, but it
generates structures amenable of being transversed in a tree
fashion. Thus, we can create objects representing the data
in the XML file easily. Let's consider the following XML file
representing a small database of aminoacids information:
Пример 2. moldb.xml - small database of molecular information <?xml version="1.0"?>
<moldb>
<molecule>
<name>Alanine</name>
<symbol>ala</symbol>
<code>A</code>
<type>hydrophobic</type>
</molecule>
<molecule>
<name>Lysine</name>
<symbol>lys</symbol>
<code>K</code>
<type>charged</type>
</molecule>
</moldb> |
|
And some code to parse the document and generate the appropriate
objects:
Пример 3.
parsemoldb.php - parses moldb.xml into an array of
molecular objects
<?php
class AminoAcid {
var $name; var $symbol; var $code; var $type; function AminoAcid ($aa)
{
foreach ($aa as $k=>$v)
$this->$k = $aa[$k];
}
}
function readDatabase($filename)
{
$data = implode("", file($filename));
$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, $data, $values, $tags);
xml_parser_free($parser);
foreach ($tags as $key=>$val) {
if ($key == "molecule") {
$molranges = $val;
for ($i=0; $i < count($molranges); $i+=2) {
$offset = $molranges[$i] + 1;
$len = $molranges[$i + 1] - $offset;
$tdb[] = parseMol(array_slice($values, $offset, $len));
}
} else {
continue;
}
}
return $tdb;
}
function parseMol($mvalues)
{
for ($i=0; $i < count($mvalues); $i++) {
$mol[$mvalues[$i]["tag"]] = $mvalues[$i]["value"];
}
return new AminoAcid($mol);
}
$db = readDatabase("moldb.xml");
echo "** Database of AminoAcid objects:\n";
print_r($db);
?>
|
|
After executing parsemoldb.php, the variable
$db contains an array of
AminoAcid objects, and the output of the
script confirms that:
xml_parse_into_struct
webmaster at unitedscripters dot com
16-Jul-2006 05:29
Ps keep in mind that some Rss feeds include spurious tags as... html entities (see Google news Rss feeds: they include tables as <table blah blah!).
If so, in my rssSnapper below add this:
<?php
$input=preg_replace("/(<!\\[CDATA\\[)|(\\]\\]>)/", '', $input);
$input=html_entity_decode($input); ?>
You may play around with the code and make it perfect, testing it on various feeds. Not _all_ XML is worth of an XML parser and the sleepless nights it entails.
webmaster at unitedscripters dot com
16-Jul-2006 02:43
It may be not entirely immaterial to stress that when you are dealing with incoming XML files such as RSS feeds, and you are about to include several of them in some page of yours, resorting to the PHP XML oriented functions is neither _necessarily_ the best idea, nor it is _strictly_ indispensable.
I have in mind, here, also a note that time ago was on this documentation by some info at gramba dot tv:
QUOTE
I was working with the xml2array functions below and had big performance problems. I fired them on a 20MB XML file and had to quit since all approaches of parsing where just too slow (more than 20 Minute parsing etc..). The solution was parsing it manually with preg_match, which increased performance by more than 20 times (processing time about 1 minute).
UNQUOTE
Calling in a specific XML structure function, and arranging a whole class, when all you want from an incoming files may be the contents of a few tags, is not the only option you are left with, when you are at PHP.
Here is a simple function that parses a XML RSS feed using no XML oriented function: keeping this in mind may spare you the need to create extremely complex classes as the ones we see here when _all_ you may want is a few titles and descriptions from an RSS (if that's your goal, you don't need XML parsers):
<?php
function rssSnapper($input='', $limit=0, $feedChannel='Yahoo!News'){
$input=file_get_contents($input);
if(!$input){return '';};
$input=preg_replace("/[\\n\\r\\t]+/", '', $input);
$input=preg_replace("/(<!\\[CDATA\\[)|(\\]\\]>)/", '', $input);
preg_match_all("/<item>(.*?)<\\/item>/", $input, $items, PREG_SET_ORDER);
$limit=(int)$limit;
$limit=($limit && is_numeric($limit) && abs($limit)<sizeof($items))? sizeof($items)-abs($limit): 0;
while(sizeof($items)>$limit){
$item=array_shift($items);
$item=$item[1];
preg_match_all("/<link>(.*?)<\\/link>/", $item, $link, PREG_SET_ORDER);
preg_match_all("/<title>(.*?)<\\/title>/", $item, $title, PREG_SET_ORDER);
preg_match_all("/<author>(.*?)<\\/author>/", $item, $author, PREG_SET_ORDER);
preg_match_all("/<pubDate>(.*?)<\\/pubDate>/", $item, $pubDate, PREG_SET_ORDER);
preg_match_all("/<description>(.*?)<\\/description>/", $item, $description, PREG_SET_ORDER);
if(sizeof($link)){ $link = strip_tags($link[0][1]); };
if(sizeof($title)){ $title = strtoupper( strip_tags($title[0][1]) ); };
if(sizeof($author)){ $author = strip_tags($author[0][1]); };
if(sizeof($pubDate)){ $pubDate = strip_tags($pubDate[0][1]); };
if(sizeof($description)){ $description = strip_tags($description[0][1]); };
print <<<USAVIT
<!-- ITEM STARTS -->
<div class="news_bg_trick">
<a href="$link" class="item" target="_blank">
<span class="title">$title<span class="channel">$feedChannel</span></span>
<span class="title_footer">
by <span class="author">$author</span> -
<span class="date">$pubDate</span>
</span>
<span class="description">$description</span>
</a>
</div>
<!-- ITEM ENDS -->
USAVIT;
}}
?>
The printing phase assigns Css class names: the output is thus fully customizable by a mere style sheet.
The use of strip_tags is a reminder from Chris Shiflett: distrust incoming data, always, anyway.
I hope no typos slipped in in transcription. Arguably not perfect, but I hope a good alternative idea to spending three days on a full fledged XML parser just to grab... three tags from a RSS feed!
bye, ALberto
efredricksen at gmail dot com
24-May-2006 12:55
Perhaps the one true parser:? I modified xademax's fine code to tidy it up, codewise and style wise, rationalize some minor crazyness, and make names fit nomenclature from the XML spec. (There are no uses of eval, and shame on you people who do.)
<?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(); $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") { $elements[$index]->children = array();
$stack[count($stack)] = &$elements;
$elements = &$elements[$index]->children;
}
}
if ($tag['type'] == "close") { $elements = &$stack[count($stack) - 1];
unset($stack[count($stack) - 1]);
}
}
return $elements[0]; }
$xml = '
<parser>
<name language="en-us">Fred Parser</name>
<category>
<name>Nomenclature</name>
<note>Noteworthy</note>
</category>
</parser>
';
print_r(xml_to_object($xml));
?>
will give:
xmlelement Object
(
[name] => parser
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => name
[attributes] => Array
(
[language] => en-us
)
[content] => Fred Parser
[children] =>
)
[1] => xmlelement Object
(
[name] => category
[attributes] =>
[content] =>
[children] => Array
(
[0] => xmlelement Object
(
[name] => name
[attributes] =>
[content] => Nomenclature
[children] =>
)
[1] => xmlelement Object
(
[name] => note
[attributes] =>
[content] => Noteworthy
[children] =>
)
)
)
)
)
Stuart
23-May-2006 03:26
This is a great little function for a lot of XML work, but note that this function does not handle XML entities properly.
The basic XML entities < > & " are fine, just anything else will not work:
If the entity is defined in the XML header, the parser will drop it completely from the struct it creates.
If the entity is not defined in the XML then the parser will crash out with an undefined entity error.
You should be able to work around this limitation by using a preg_replace on your XML string before passing it to the parser.
(Further details in Bug #35271; this is just a warning to those thinking of using this function for parsing real XML documents not just trivial XML examples)
donna at coloma dot com
28-Apr-2006 12:15
I needed a very simple parser for a set of name-value pairs to be stored in a single database field. I started with the mold example, paired it down, picked up the "id" attributes. Perhaps it will be useful for someone else.
<?php
$xmlInput = "<?xml version=\"1.0\"?>
<mcw_settings>
<field id=\"imageAlign\">left</field>
<field id=\"caption\">What a nice picture.</field>
</mcw_settings>";
$desiredResult = array (
'imageAlign' => "left",
'caption' => "What a nice picture."
);
function parseFields ($data)
{
$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, $data, $values, $tags);
xml_parser_free($parser);
$fields = null;
$fieldIndices = $tags['field'];
for ( $i=0; $i < count($fieldIndices); $i++ ) {
$fieldInfo = $values[$fieldIndices[$i]];
$fields[$fieldInfo['attributes']['id']] = $fieldInfo['value'];
}
return $fields;
}
$test = parseFields($xmlInput);
echo "** Result:\n";
print_r($test);
?>
matt at australiangamer dot com
02-Apr-2006 07:58
I liked VampBoy's code as it gave me structure pretty much as I wanted it. Just two notes, though:
There is a bug in this code. subdivide() checks for pre-existing values, but not in the case of a "complete" element.
In the following (appallingly bad) XML
<Names>
<Name>Matt</Name>
<Name>Stacy</Name>
<Names>
Only Stacy is added to Names in the array, wiping Matt in the process.
To fix this replace the following code
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
$newarray[$dat[tag]]=$dat[value];
}
?>
with
<?php
elseif ($dat[level] === $level && $dat[type] === "complete"){
if (isset($newarray[$dat['tag']]) && is_array($newarray[$dat['tag']])){
$newarray[$dat['tag']][] = $dat['value'];
} elseif (isset($newarray[$dat['tag']]) && !is_array($newarray[$dat['tag']])){
$newarray[$dat['tag']] = array($newarray[$dat['tag']], $dat['value']);
} else {
$newarray[$dat['tag']]=$dat['value'];
}
}?>
Oh, also, note that the $dat[level], etc, WILL generate warnings, and should be more correctly written as $dat['level'].
The other thing I thought I should point out is that the array keys when created using xml_parse_into_struct will be an UPPERCASE version of your existing element names. If case is important, especially if you, like me, need mixedCase, do the following:
<?php
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,0);
?>
29-Mar-2006 11:57
$simple = '<?xml version="1.0"?>
<moldb>
<molecule>
<name>Alanine</name>
<symbol>ala</symbol>
<code>A</code>
<type>hydrophobic</type>
</molecule>
<molecule>
<name>Lysine</name>
<symbol>lys</symbol>
<code>K</code>
<type>charged</type>
</molecule>
</moldb>';
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "<pre>";
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
foreach ($vals as $k => $v) {
$i = 0;
if (trim($v['value']) != '') {
if ($arr[$i][$v['tag']] != '') {
$i ++;
}
$arr[$i][$v['tag']] = $v['value'];
}
}
xademax at gmail dot com
08-Jan-2006 12:15
This is just another simple xml parser :)
<?php
class Xml
{
var $tag;
var $value;
var $attributes;
var $next;
}
function xml2array($xml_string)
{
$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_string, $Xml_Values);
xml_parser_free($Parser);
$XmlClass = array();
$LastObj = array();
$NowObj = &$XmlClass;
foreach($Xml_Values as $Xml_Key => $Xml_Value)
{
$Index = count($NowObj);
if($Xml_Value["type"] == "complete")
{
$NowObj[$Index] = new Xml;
$NowObj[$Index]->tag = $Xml_Value["tag"];
$NowObj[$Index]->value = $Xml_Value["value"];
$NowObj[$Index]->attributes = $Xml_Value["attributes"];
}
elseif($Xml_Value["type"] == "open")
{
$NowObj[$Index] = new Xml;
$NowObj[$Index]->tag = $Xml_Value["tag"];
$NowObj[$Index]->value = $Xml_Value["value"];
$NowObj[$Index]->attributes = $Xml_Value["attributes"];
$NowObj[$Index]->next = array();
$LastObj[count($LastObj)] = &$NowObj;
$NowObj = &$NowObj[$Index]->next;
}
elseif($Xml_Value["type"] == "close")
{
$NowObj = &$LastObj[count($LastObj) - 1];
unset($LastObj[count($LastObj) - 1]);
}
}
return $XmlClass;
}
$String = "
<parser>
<parseur_name>MyParser</parseur_name>
<category>
<name>Name 1</name>
<note>A note 1</note>
</category>
</parser>
";
$Xml = xml2array($String);
print_r($Xml);
?>
This exemple will show :
Array
(
[0] => Xml Object
(
[tag] => parser
[value] =>
[attributes] =>
[next] => Array
(
[0] => Xml Object
(
[tag] => parseur_name
[value] => MyParser
[attributes] =>
[next] =>
)
[1] => Xml Object
(
[tag] => category
[value] =>
[attributes] =>
[next] => Array
(
[0] => Xml Object
(
[tag] => name
[value] => Name 1
[attributes] =>
[next] =>
)
[1] => Xml Object
(
[tag] => note
[value] => A note 1
[attributes] =>
[next] =>
)
)
)
)
)
)
VampBoy
14-Dec-2005 02:45
WHUPS! that was a broken test version.here is the real one:
class xml2array{
/* This class parses XML tags into a recursive, associative array with the tags as the associative array elements names.
if it encounters multiples of the same tag within a stream, it enumerates them as a sub array under the tag thus:
Array (
[Lvl1tag] => Array (
[0] => Array(
[Lvl2tag] = "foo")
[1]=> Array(
[Lvl2tag] = "bar")
)
)
It tries to detect when there is only one copy of a tag under another, and concatinate properly.
*/
function readxmlfile($xmlfile){ // reads XML file in and returns it
$xmlstream =fopen($xmlfile,r);
$xmlraw=fread($xmlstream,1000000);
fclose($xmlstream);
return $xmlraw;
}
function parseXMLintoarray ($xmldata){ // starts the process and returns the final array
$xmlparser = xml_parser_create();
xml_parse_into_struct($xmlparser, $xmldata, $arraydat);
xml_parser_free($xmlparser);
$semicomplete = $this->subdivide($arraydat);
$complete = $this->correctentries($semicomplete);
return $complete;
}
function subdivide ($dataarray, $level = 1){
foreach ($dataarray as $key => $dat){
if ($dat[level] === $level && $dat[type] === "open"){
$toplvltag = $dat[tag];
} elseif ($dat[level] === $level && $dat[type] === "close" && $dat[tag]=== $toplvltag){
$newarray[$toplvltag][] = $this->subdivide($temparray,($level +1));
unset($temparray,$nextlvl);
} elseif ($dat[level] === $level && $dat[type] === "complete"){
$newarray[$dat[tag]]=$dat[value];
} elseif ($dat[type] === "complete"||$dat[type] === "close"||$dat[type] === "open"){
$temparray[]=$dat;
}
}
return $newarray;
}
function correctentries($dataarray){
if (is_array($dataarray)){
$keys = array_keys($dataarray);
if (count($keys)== 1 && is_int($keys[0])){
$tmp = $dataarray[0];
unset($dataarray[0]);
$dataarray = $tmp;
}
$keys2 = array_keys($dataarray);
foreach($keys2 as $key){
$tmp2 = $dataarray[$key];
unset($dataarray[$key]);
$dataarray[$key] = $this->correctentries($tmp2);
unset($tmp2);
}
}
return $dataarray;
}
}
p dot gasiorowski at axent dot pl
13-Dec-2005 06:31
Something similar to kieran's _xml2array, however much more faster.
------
class ObjectFromXML
{
var $parser;
var $iter = 0;
var $path = array();
var $xml = array();
function ObjectFromXML($XML)
{
$this->parser = xml_parser_create();
xml_set_object($this->parser, &$this);
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($this->parser, "hanleTagStart", "hanleTagEnd");
xml_set_character_data_handler($this->parser, "hanleTagCData");
xml_parse($this->parser, $XML);
xml_parser_free($this->parser);
$this->xml = $this->xml['_children'][0];
}
function getEvalPath()
{
return '$this->xml[' . "'" . implode("']['", $this->path) . "'" . ']';
}
function hanleTagStart($parser, $tag, $attributes)
{
array_push($this->path, '_children');
array_push($this->path, ($this->iter++));
$e = $this->getEvalPath();
eval ($e . "['_name'] = \$tag;");
if ($attributes !== array())
{
eval ($e . "['_attributes'] = \$attributes;");
}
}
function hanleTagCData($parser, $cdata)
{
$e = $this->getEvalPath();
eval ($e . "['_value'] = \$cdata;");
}
function hanleTagEnd($parser, $tag)
{
array_pop($this->path);
array_pop($this->path);
}
}
mbirth at webwriters dot de
08-Nov-2005 03:48
Searching for a nice and working way to get a RSS feed into an array-structure, I found the solution posted by kieran but disliked those several eval()s. So I wrote my own using references/pointers.
<?php
class RSSParser {
var $struct = array(); var $curptr; var $parents = array(); function RSSParser($url) {
$this->curptr =& $this->struct; $xmlparser = xml_parser_create();
xml_set_object($xmlparser, $this);
xml_set_element_handler($xmlparser, 'tag_open', 'tag_close');
xml_set_character_data_handler($xmlparser, 'cdata');
$fp = fopen($url, 'r');
while ($data = fread($fp, 4096))
xml_parse($xmlparser, $data, feof($fp))
|| die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xmlparser)),
xml_get_current_line_number($xmlparser)));
fclose($fp);
xml_parser_free($xmlparser);
}
function tag_open($parser, $tag, $attr) {
$i = count($this->curptr['children']);
$j = count($this->parents);
$this->curptr['children'][$i]=array(); $this->parents[$j] =& $this->curptr; $this->curptr =& $this->curptr['children'][$i]; $this->curptr['name'] = $tag;
if (count($attr)>0) $this->curptr['attr'] = $attr;
}
function tag_close($parser, $tag) {
$i = count($this->parents);
if ($i>0) $this->curptr =& $this->parents[$i-1]; unset($this->parents[$i-1]); }
function cdata($parser, $data) {
$data = trim($data);
if (!empty($data)) {
$this->curptr['value'] .= $data;
}
}
}
$myparser = new RSSParser('getitems.xml');
$anotherparser = new RSSParser('http://johndoe:secret@myfeeds.com/getfeed.xml');
print_r($myparser->struct);
print_r($anotherparser->struct);
?>
kieran at kieran dot ca
20-Oct-2005 09:20
<?
function _xml2array($xml){
global $keys;
global $level;
if(!is_array($xml)){ $raw_xml = $xml;
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $raw_xml, $xml, $idx);
xml_parser_free($p);
}
for($i=0;$i<count($xml,1);$i++){
$level = $xml[$i]['level'];
if($level<1)break;
$keys[$level] = '['.$i.']';
if(count($keys)>$level) unset($keys[count($keys)]);
if($xml[$i]['type']=="open" || $xml[$i]['type']=="complete"){
$e = '$output'.implode('[\'_children\']',$keys);
eval($e.'[\'_name\'] = $xml[$i][\'tag\'];');
if($xml[$i]['attributes']){
eval($e.'[\'_attributes\'] = $xml[$i][\'attributes\'];');
}
if($xml[$i]['value']){
eval($e.'[\'_value\'] = trim($xml[$i][\'value\']);');
}
}
}
return $output;
}
?>
info at gramba dot tv
23-Aug-2005 08:54
I was working with the xml2array functions below and had big performance problems. I fired them on a 20MB XML file and had to quit since all approaches of parsing where just too slow (more than 20 Minute parsing etc..). The solution was parsing it manually with preg_match, which increased performance by more than 20 times (processing time about 1 minute).
Rough example function with high performance:
<?php
function customXMLtoARRAY($xmlstring) {
preg_match_all("#<node>(.*?)</node>#s",$xmlstring,$nodes);
$xmlstring = NULL;
$allnodes = array();
while($nodes = array_pop($nodes[1])) {
$nodecontent = array();
preg_match("#<content1>(.*?)</content1>#",$eventreihe,$val);
$nodecontent['content1'] = $val[1];
$allnodes[] = $nodecontent;
}
return $allnodes;
}
?>
Chris Hester
28-Jul-2005 05:06
The array generated from XML stores not only the elements but also any spaces and linebreaks between the tags. This results in a much longer array. (I had 24 array fields instead of 10!) To cure this use the following code when creating the parser:
<?php
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser,XML_OPTION_SKIP_WHITE,1);
?>
grusin at gmail dot com
19-Jul-2005 11:54
Here is a simple patch to peter's xml2array function.
Replace:
<?
case 'open':
$tag_or_id = (array_key_exists ('attributes', xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
$ptrs[$level][$tag_or_id] = array ();
$ptrs[$level+1] = & $ptrs[$level][$tag_or_id];
break;
?>
with:
<?
case 'open':
$tag_or_id = (array_key_exists ('attributes', $xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
$ptrs[$level][$tag_or_id][] = array ();
$ptrs[$level+1] = & $ptrs[$level][$tag_or_id][count($ptrs[$level][$tag_or_id])-1];
break;
?>
and now code should handle multiple element case :)
Dustin
13-Jul-2005 08:39
If you happen to have problems with weird charaters, I added this code to the first line of dUDA's function:
$XML = utf8_decode($XML);
PhF at madmac dot fr
23-Jun-2005 01:11
The code previously posted by noob at noobsrule dot com doesn't work when the same tag name is used at different levels.
(but perhaps "$php_used_prefix" was intended for that ?)
For example:
<RatedShipment>
<TransportationCharges>
...
</TransportationCharges>
<RatedPackage>
<TransportationCharges>
...
</TransportationCharges>
</RatedPackage>
</RatedShipment>
<?php
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $UPSxml, $values, $idx);
xml_parser_free($p);
$usedKeys = array();
$deepLevel = -1;
$forEvalPrefix = '$xml_array';
foreach ($values as $key => $val) {
$tagName = $val['tag']; $level = $val['level']; if($val['type'] == 'open') {
$deepLevel++; $forEvalPrefix .= '[\''. $tagName .'\']';
(isset($usedKeys[$level][$tagName])) ? $usedKeys[$level][$tagName]++ : $usedKeys[$level][$tagName] = 0;
$forEvalPrefix .= '['. $usedKeys[$level][$tagName] .']';
}
if($val['type'] == 'complete') {
($level > $deepLevel) ? $deepLevel++ : ''; $tagValue = addslashes($val['value'] ); $forEvalSuffix = '[\''. $tagName .'\'] = \''. $tagValue .'\';'; $forEval = $forEvalPrefix . $forEvalSuffix; eval($forEval); }
if($val['type'] == 'close') {
unset($usedKeys[$deepLevel]); $deepLevel--;
$forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); $forEvalPrefix = substr($forEvalPrefix, 0, strrpos($forEvalPrefix, '[')); }
}
echo '<pre>';
print_r ($xml_array);
echo '</pre>';
?>
MADmac ;-)
peter at elemental dot org
15-Jun-2005 08:46
Quick addendum to my post below... The code does *not* handle cases where multiple elements are used without attribute IDs, as is the case with the moldb example. (It is probably worth noting that some of the other code below doesn't do this either, and is still more complex.)
It is not difficult to add that functionality, but I have yet to find an elegant way to integrate that feature, and since I don't need that functionality at the moment, I'm going leave that as an exercise to the reader. ;-)
-p.
peter at elemental dot org
14-Jun-2005 04:09
Some of the code posted previously seemed absurdly complex, so I thought I'd see if I could simplify things a bit. Here's what I came up with. (I'm still a PHP newbie, so perhaps I simplified too much. ;-) Untested for speed, but it *should* be pretty quick since it uses references instead of lookups or (gack) evals.
Enjoy!
/*
* Convert a string containing XML into a nested array
*
* Takes a raw XML string as input
* Returns a nested array
*/
function xml2array ($xml_data)
{
// parse the XML datastring
$xml_parser = xml_parser_create ();
xml_parse_into_struct ($xml_parser, $xml_data, $vals, $index);
xml_parser_free ($xml_parser);
// convert the parsed data into a PHP datatype
$params = array();
$ptrs[0] = & $params;
foreach ($vals as $xml_elem) {
$level = $xml_elem['level'] - 1;
switch ($xml_elem['type']) {
case 'open':
$tag_or_id = (array_key_exists ('attributes', $xml_elem)) ? $xml_elem['attributes']['ID'] : $xml_elem['tag'];
$ptrs[$level][$tag_or_id] = array ();
$ptrs[$level+1] = & $ptrs[$level][$tag_or_id];
break;
case 'complete':
$ptrs[$level][$xml_elem['tag']] = (isset ($xml_elem['value'])) ? $xml_elem['value'] : '';
break;
}
}
return ($params);
}
gleber at mapnet dot pl
26-Mar-2005 03:03
XMLtoArray created by dudus at onet dot pl was not good enough for me, that's why i decided to create my own version, and here's the result:
<?php
function &last(&$array) {
if (!count($array)) return null;
end($array);
return $array[key($array)];
}
function myParseXML(&$vals, &$dom, &$lev) {
do {
$curr = current($vals);
$lev = $curr['level'];
switch ($curr['type']) {
case 'open':
if (isset($dom[$curr['tag']])) {
$tmp = $dom[$curr['tag']];
if (!$tmp['__multi'])
$dom[$curr['tag']] = array('__multi' => true, $tmp);
array_push($dom[$curr['tag']], array());
$new =& last($dom[$curr['tag']]);
} else {
$dom[$curr['tag']] = array();
$new =& $dom[$curr['tag']];
}
next($vals);
myParseXML(&$vals, $new, $lev);
break;
case 'cdata':
break;
case 'complete':
if (!isset($dom[$curr['tag']]))
$dom[$curr['tag']] = $curr['value'];
else {
if (is_array($dom[$curr['tag']]))
array_push($dom[$curr['tag']] , $curr['value']);
else
array_push($dom[$curr['tag']] = array($dom[$curr['tag']]) , $curr['value']);
}
break;
case 'close':
return;
}
}
while (next($vals)!==FALSE);
}
function MyXMLtoArray($XML) {
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $XML, $vals);
xml_parser_free($xml_parser);
reset($vals);
$dom = array(); $lev = 0;
myParseXML($vals, $dom, $lev);
return $dom;
}
?>
MyXMLtoArray() is that function you should call, other are for "internal" use.
P.S. It's 4-5 time faster then dudus at onet dot pl 's code
noob at noobsrule dot com
24-Jan-2005 10:59
This is my first time contributing but I found all the examples either didn't work or were beyond my comprehension. I also wanted to maintain the XML multi-dimensionality so I could do loops based on keys I was expecting rather than numbers or some other whatnot. This script is probably way inefficient, but should give you a multidimensional array with a depth that will match the XML source. Great for situations when you aren't sure what you'll be getting back. Its also only based on the values array of xml_parse_into_str() using the 3 tag types (open, complete, and close) -- hopefully it will make more sense to other noobs.
Basically it just constructs a string ($php_prefix) that is tweaked if it hits an "open" or "close" tag and evaluated if it hits a "complete" tag.
Thanks.
<?php
$ch = curl_init( "http://yourtargethost" ) or die ( "could not initialize curl" );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $request_str );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 0 );
curl_setopt( $ch, CURLOPT_REFERER, https:curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$curl_result = curl_exec( $ch ) or die ( "could not execute the post" );
curl_close( $ch ); $p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $curl_result, $vals, $index);
xml_parser_free($p);
$used_keys = array();
$php_prefix = "$"."my_array";
foreach ($vals as $key => $val) {
if( $val['type'] == "open" ) {
$my_tag = $val['tag']; $php_prefix .= "['".$my_tag."']"; if( ! array_key_exists( $my_tag, $used_keys ) ) {
$used_keys[$my_tag] = 0;
$php_prefix .= "[".$used_keys[$my_tag]."]";
}else {
$used_keys[$my_tag]++; $php_prefix .= "[".$used_keys[$my_tag]."]";
}
} elseif( $val['type'] == "complete" ) {
$my_value = addslashes( $val['value'] ); $php_suffix = "['".$val['tag']."'] = \"".$my_value."\";"; $php_str = $php_prefix.$php_used_prefix.$php_suffix;
eval( $php_str ); }
if( $val['type'] == "close" ) {
$php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); $php_prefix = substr( $php_prefix, 0, strrpos( $php_prefix, "[" ) ); }
}
echo "<pre>";
print_r ($my_array);
echo "</pre>";
?>
dudus at onet dot pl
18-Jan-2005 06:33
This function return array from XML. Included attributes and tags in same level.
I wrote this function based on mmustafa code.
enjoy!
dUDA
-------------------------------
<?
function XMLtoArray($XML)
{
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $XML, $vals);
xml_parser_free($xml_parser);
$_tmp='';
foreach ($vals as $xml_elem)
{
$x_tag=$xml_elem['tag'];
$x_level=$xml_elem['level'];
$x_type=$xml_elem['type'];
if ($x_level!=1 && $x_type == 'close')
{
if (isset($multi_key[$x_tag][$x_level]))
$multi_key[$x_tag][$x_level]=1;
else
$multi_key[$x_tag][$x_level]=0;
}
if ($x_level!=1 && $x_type == 'complete')
{
if ($_tmp==$x_tag)
$multi_key[$x_tag][$x_level]=1;
$_tmp=$x_tag;
}
}
foreach ($vals as $xml_elem)
{
$x_tag=$xml_elem['tag'];
$x_level=$xml_elem['level'];
$x_type=$xml_elem['type'];
if ($x_type == 'open')
$level[$x_level] = $x_tag;
$start_level = 1;
$php_stmt = '$xml_array';
if ($x_type=='close' && $x_level!=1)
$multi_key[$x_tag][$x_level]++;
while($start_level < $x_level)
{
$php_stmt .= '[$level['.$start_level.']]';
if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
$php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
$start_level++;
}
$add='';
if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type=='open' || $x_type=='complete'))
{
if (!isset($multi_key2[$x_tag][$x_level]))
$multi_key2[$x_tag][$x_level]=0;
else
$multi_key2[$x_tag][$x_level]++;
$add='['.$multi_key2[$x_tag][$x_level].']';
}
if (isset($xml_elem['value']) && trim($xml_elem['value'])!='' && !array_key_exists('attributes',$xml_elem))
{
if ($x_type == 'open')
$php_stmt_main=$php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
else
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
eval($php_stmt_main);
}
if (array_key_exists('attributes',$xml_elem))
{
if (isset($xml_elem['value']))
{
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
eval($php_stmt_main);
}
foreach ($xml_elem['attributes'] as $key=>$value)
{
$php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
eval($php_stmt_att);
}
}
}
return $xml_array;
} ?>
memandeemail at gmail dot com
09-Dec-2004 07:09
a BREAK on right place, will be faster and bugprevent
/**
* @return bool
* @param xml_array $values
* @param xml_array[reference] $tags
* @desc Reconstruct xml_parse_into_struct to open and close concept
*/
function reconstruct_tag($values, &$tags) {
unset($return);
foreach ($tags as $tkey => $tvalue) {
foreach ($tvalue as $vvalue) {
if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') {
$return[$tkey][] = $vvalue;
}
if ($values[$vvalue]['type'] == 'open') {
foreach ($tvalue as $tv_value) {
if ($values[$tv_value]['type'] == 'close'
and $values[$tv_value]['level'] == $values[$vvalue]['level']
and $tv_value > $vvalue) {
$return[$tkey][] = $tv_value;
break;
}
}
}
}
}
$tags = $return;
return (bool) $tags;
}
memandeemail at gmail dot com
03-Dec-2004 04:32
If you have de XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<teste>
<teste>
<teste>
</teste>
</teste>
</teste>
this function will return this tags:
Array
(
[teste] => Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
)
)
With this function (right below) will convert the struc to OPEN and CLOSE concept.
The New Struct:
Array
(
[teste] => Array
(
[0] => 0
[1] => 4
[2] => 1
[3] => 3
[4] => 2
)
)
The Function:
/**
* @return bool
* @param xml_array $values
* @param xml_array[reference] $tags
* @desc Reconstruct xml_parse_into_struct
*/
function reconstruct_tag($values, &$tags) {
unset($return);
foreach ($tags as $tkey => $tvalue) {
foreach ($tvalue as $vvalue) {
if ($values[$vvalue]['type'] == 'open' or $values[$vvalue]['type'] == 'complete') {
$return[$tkey][] = $vvalue;
}
if ($values[$vvalue]['type'] == 'open') {
foreach ($tvalue as $tv_value) {
if ($values[$tv_value]['type'] == 'close'
and $values[$tv_value]['level'] == $values[$vvalue]['level']) {
$return[$tkey][] = $tv_value;
}
}
}
}
}
$tags = $return;
return (bool) $tags;
}
Ahh, DO NOT forget this comment:
jukkaho at mail dot student dot oulu dot fi
29-Jun-2004 12:17
Everyone seems to forget the return value of this function (including people making examples!), but it is there for a reason:
0 if failed (invalid XML), 1 if succeeded.
Return value really is an integer, be careful with those === operators.
rob at digital hyphen crocus dot com
03-Sep-2004 09:21
I wrote this function in a bad mood, to get some data you can loop through. sprint_r or var_dump its output to see what it does. It's not pretty, but at least you can loop through the data without a LOT of hassle.
This can also be done with handlers, but I couldn't be bothered with global variables.
<?php
$data = xml_parse_into_struct($somexml);
function xml_getmeaningfuldata($data) {
foreach($data as $element) {
if ($element['type'] == "open") {
if (!$elementnos[$parents.$element['tag']]) {
$elementnos[$parents.$element['tag']] = 1;
}
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['attributes'] = $element['attributes'];
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['value'] = trim($element['value']);
$parents .= $element['tag'].":".$elementnos[$parents.$element['tag']]."^";
}
if ($element['type'] == "cdata") {
if (trim($element['value'])) {
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['values'] = $element['value'];
}
}
if ($element['type'] == "complete") {
if (!$elementnos[$parents.$element['tag']]) {
$elementnos[$parents.$element['tag']] = 1;
}
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['attributes'] = $element['attributes'];
$elements[$parents.$element['tag'].":". $elementnos[$parents.$element['tag']]]['value'] = trim($element['value']);
$elementnos[$parents.$element['tag']]++;
}
if ($element['type'] == "close") {
$parents = substr($parents, 0, strrpos(substr($parents, 0, -1), "^")+1);
$elementnos[$parents.$element['tag']]++;
}
}
return $elements;
}
happyseb at free dot fr
24-Aug-2004 08:51
I would just add something about the solution proposed by "gordon at kanazawa dot ac dot jp".
Something went wrong about the fact that "cdata" are treated the same way as "complete" in the xml_get_children first and in the xml_get_child then.
When ?
with this method, something like this :
<tagA>
<tagB>valueB</tagB>
valueA
<tagC>valueC</tagC>
</tagA>
is considered as the same thing as
<tagA>
<tagB>valueB</tagB>
<tagA>valueA</tagA>
<tagC>valueC</tagC>
</tagA>
The algorithm is nomore bijective.
Even if it might not be perfect :-) i propose a little modification of the xml_get_child function :
function xml_get_child(&$v, $children=NULL) {
$c = array(); // the "child"
if (isset($v['tag']) && $v['type'] != 'cdata') $c['tag'] = $v['tag'];
if (isset($v['value'])) $c['value'] = $v['value'];
if (isset($v['attributes'])) $c['attributes'] = $v['attributes'];
if (is_array($children)) $c['children'] = $children;
return $c;
}
moreover, the following function will take the tree (made from the xml_get_tree function) as a parameter and build the xml content back
/*
// setTree
// set xml Tree from a well-formed array
// manage indentation
// Parameters are :
// $1 (needed) : Tree to transform
// $2 (optional) : Indentation type (default is one tabulation)
// $3 (optional) : First indent (default is empty string)
*/
function setTree($Atree, $indentType = ' ', $Sindent = '')
{
if (! isset ($Atree['tag']) && isset ($Atree['value'])) return ($Sindent.trim($Atree['value'])."\r\n") ;
else {
$Stemp = $Sindent.'<'.$Atree['tag'].' ' ;
if (isset ($Atree['attributes']) && is_array ($Atree['attributes'])) {
foreach ($Atree['attributes'] as $Aattribute => $Svalue) $Stemp .= $Aattribute.'="'.$Svalue.'" ' ;
}
if (! isset ($Atree['children']) && ! isset ($Atree['value'])) $Stemp .= '/>'."\r\n" ;
else {
$Stemp .= '>'."\r\n" ;
if (isset ($Atree['value'])) $Stemp .= $Sindent.$indentType.trim($Atree['value'])."\r\n" ;
if (isset ($Atree['children']) && is_array ($Atree['children']))
foreach ($Atree['children'] as $Achild) $Stemp .= $this->setTree($Achild, $indentType, $Sindent.$indentType) ;
$Stemp .= $Sindent.'</'.$Atree['tag'].'>'."\r\n" ;
}
return ($Stemp) ;
}
}
sdh33 at cornell dot edu
13-Jun-2004 10:12
(The previous post I wrote was incorrect. I fix that herein)
I used the algorithm from stephane for writing back to a file, but ran into the following problem. My original XML looks like
<ENTRY id="1"> ... </ENTRY><ENTRY id="2"> ... </ENTRY>
This got saved back as
<1> ... </1><2> ... </2>,
which expat doesn't like. So I propose the following modification:
function writeXmlToFile($data, $f, $niv = 0) {
foreach($data as $name => $elem) {
if (preg_match("/\A[0-9]/",$name)) {
$open = "ENTRY id=\"$name\""; $name = "ENTRY";
} else {
$open = $name;
}
if(is_array($elem)) {
fwrite($f, str_repeat(" ", $niv)."<".$open.">"."\n");
writeXmlToFile($elem, $f, $niv + 1);
fwrite($f, str_repeat(" ", $niv)."</".$name.">"."\n");
} else {
fwrite($f, str_repeat(" ", $niv)."<".$open.">".$elem."</".$name.">"."\n");
}
}
}
stephane dot dekeyzer at noemail dot com
08-Apr-2004 11:54
for writing the array of mmustafa back to a xml file, use this code:
function writeXmlFile($fileName, $struct){
$f = fopen($fileName, "w");
writeXmlToFile($struct, $f);
fclose($f);
}
function writeXmlToFile($data, $f, $niv = 0){
foreach($data as $name => $elem){
if(is_array($elem)){
fwrite($f, str_repeat(" ", $niv)."<".$name.">"."\n");
writeXmlToFile($elem, $f, $niv + 1);
fwrite($f, str_repeat(" ", $niv)."</".$name.">"."\n");
}
else{
fwrite($f, str_repeat(" ", $niv)."<".$name.">".$elem."</".$name.">"."\n");
}
}
}
gordon at kanazawa dot ac dot jp
25-Jan-2004 08:36
Many thanks to "dave at ovumdesign dot com" for his excellent routines based on the fine work of "gdemartini at bol dot com". I offer a further refinement below, which removes the duplication of the code to create a child node. The resulting tree is the same as in Dave's earlier version and so can be compacted using his "xml_condense_tree"
function xml_get_child(&$v, $children=NULL) {
$c = array(); // the "child"
if (isset($v['tag'])) $c['tag'] = $v['tag'];
if (isset($v['value'])) $c['value'] = $v['value'];
if (isset($v['attributes'])) $c['attributes'] = $v['attributes'];
if (is_array($children)) $c['children'] = $children;
return $c;
}
function xml_get_children(&$values, &$i) {
$children = array();
while ($i < count($values)) {
$v = &$values[$i++];
switch ($v['type']) {
case 'cdata':
case 'complete':
$children[] = xml_get_child($v);
break;
case 'open':
$children[] = xml_get_child($v, xml_get_children($values, $i));
break;
case 'close':
break 2; // leave "while" loop
}
}
return $children;
}
function xml_get_tree($file) {
$data = join('', file($file));
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $data, $values);
xml_parser_free($p);
return xml_get_children($values, $i=0);
}
mmustafa at vsnl dot com
06-Jan-2004 12:19
A small and simple php code to convert the raw array returned by xml_parse_into_struct into a proper data structure ..
<?php
$file = "data.xml";
$xml_parser = xml_parser_create();
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
$data = fread($fp, filesize($file));
fclose($fp);
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);
$params = array();
$level = array();
foreach ($vals as $xml_elem) {
if ($xml_elem['type'] == 'open') {
if (array_key_exists('attributes',$xml_elem)) {
list($level[$xml_elem['level']],$extra) = array_values($xml_elem['attributes']);
} else {
$level[$xml_elem['level']] = $xml_elem['tag'];
}
}
if ($xml_elem['type'] == 'complete') {
$start_level = 1;
$php_stmt = '$params';
while($start_level < $xml_elem['level']) {
$php_stmt .= '[$level['.$start_level.']]';
$start_level++;
}
$php_stmt .= '[$xml_elem[\'tag\']] = $xml_elem[\'value\'];';
eval($php_stmt);
}
}
echo "<pre>";
print_r ($params);
echo "</pre>";
?>
Converts an XML document like ..
<country id="ZZ">
<name>My Land</name>
<location>15E</location>
<area>40000</area>
<state1>
<name>Hi State</name>
<area>1000</area>
<population>2000</population>
<city1>
<location>13E</location>
<population>500</population>
<area>500</area>
</city1>
<city2>
<location>13E</location>
<population>500</population>
<area>5000</area>
</city2>
</state1>
<state2>
<name>Low State</name>
<area>3000</area>
<population>20000</population>
<city1>
<location>15E</location>
<population>5000</population>
<area>1500</area>
</city1>
</state2>
</country>
To a php datastructure like ..
Array
(
[ZZ] => Array
(
[NAME] => My Land
[LOCATION] => 15E
[AREA] => 40000
[STATE1] => Array
(
[NAME] => Hi State
[AREA] => 1000
[POPULATION] => 2000
[CITY1] => Array
(
[LOCATION] => 13E
[POPULATION] => 500
[AREA] => 500
)
[CITY2] => Array
(
[LOCATION] => 13E
[POPULATION] => 500
[AREA] => 5000
)
)
[STATE2] => Array
(
[NAME] => Low State
[AREA] => 3000
[POPULATION] => 20000
[CITY1] => Array
(
[LOCATION] => 15E
[POPULATION] => 5000
[AREA] => 1500
)
)
)
)
26-Sep-2003 03:43
actually, using the notation
$object->property is the right one
and you can use it always, preventing errors with arrays
Eric
22-Sep-2003 08:50
Yet another XML -> nested array converter.
This is based on http://www.devdump.com/phpxml.php, but adds features while reducing code complexity.
It can take several forms of input:
- string (buffer)
- file handle (pipe)
- file name -or-
- URL
It can optionally collapse arrays of same-named tags to a single element if there is only one tag with a given name.
It can optionally preserve tag order for XML applications where position is significant, such as CallXML.
XMLParser.obj at:
http://eric.pollmann.net/work/public_domain/
Corrections and suggestions enthusiastically welcomed. :)
Adam Tylmad
12-Sep-2003 05:01
I've just debugged and optimized my xmlparser.
gdemartini's xml-example generates this structure:
stdClass Object
(
[FORM] => stdClass Object
(
[SECTION] => stdClass Object
(
[NAME] => Data
[EDITFIELD] => stdClass Object
(
[LABEL] => Text:
[NAME] => poll_text
[LENGTH] => 255
[SIZE] => 56
[REQUIRED] => T
)
[MEMOFIELD] => stdClass Object
(
[LABEL] => Options
[NAME] => options
[COLS] => 56
[ROWS] => 5
[REQUIRED] => T
)
)
)
)
the moldb example generates this structure:
stdClass Object
(
[MOLDB] => stdClass Object
(
[MOLECULE] => Array
(
[0] => stdClass Object
(
[NAME] => Alanine
[SYMBOL] => ala
[CODE] => A
[TYPE] => hydrophobic
)
[1] => stdClass Object
(
[NAME] => Lysine
[SYMBOL] => lys
[CODE] => K
[TYPE] => charged
)
)
)
)
here is the code. enjoy!
<?php
class XMLParser {
var $path;
var $result;
function cleanString($string) {
return trim(str_replace("'", "'", $string));
}
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) {
$this->path .= "->".$tag;
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]";
}
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) {
if ($data = XMLParser::cleanString($data))
eval($this->path." = '$data';");
}
}
?>
plusplus7 at hotmail dot com
18-Jun-2003 06:40
Quick and dirty function to get original file from $vals array in manual example. Build on this for custom parsing of individual elements (something like this should be built-in IMO).
<pre>
function parseVals($vals){
foreach($vals as $valobj){
if($valobj['type']=='open' || $valobj['type']=='complete'){
$retstr .= '<'.$valobj['tag'];
if(is_array($valobj['attributes'])){
foreach($valobj['attributes'] as $key=>$val){
$retstr .= " $key=\"$val\"";
}
}
}
if($valobj['type']=='open'){
$retstr .= '>'.$valobj['value'];
}elseif($valobj['type']=='complete' && $valobj['value']){
$retstr .= '>'.$valobj['value'].'</'.$valobj['tag'].'>';
}elseif($valobj['type']=='complete'){
$retstr .= ' />';
}elseif($valobj['type']=='cdata'){
$retstr .= $valobj['value'];
}elseif($valobj['type']=='close'){
$retstr .= '</'.$valobj['tag'].'>';
}
}
return $retstr;
}
</pre>
masuod_a at hotmail dot com
24-Nov-2002 04:32
wanna load object from xml file? this function take a tree returned by xml_get_tree() [dave@ovumdesign.com's version] and return an object.
it can't work with attributes, i hate attributes (except id), but may be useful for you.
- how to use :
------------------------- xml file -------------
<config>
<images>
<image id="1">
<name>my pet</name>
<file>pet001.jpg</file>
</image>
<image id="2">
<name>harry's pet</name>
<file>pet003.jpg</file>
</image>
</images>
<library>lib_functions.php</library>
</config>
----------------------------------------------
$config=xml_to_object($xml_tree);
$config->library; #return "lib_functions.php"
$config->images->image__1->name; #return "my pet"
----------------------------------------------
function xml_to_object($xml_tree,$root=false){
$object_name=$xml_tree['tag'];
foreach ($xml_tree['children'] as $children){
$child_id=$children['attributes']['id'];
$child_name=$children['tag'];
$child_name.=($child_id) ? "__".$child_id:'';
if (is_array($children['children'])) {
$child_object=xml_to_object($children);
$temp_object->$child_name=$child_object;
} else {
$temp_object->$child_name=$children['value'];
}
}
if (!$root)
$xml_object=$temp_object;
else $xml_object->$object_name=$temp_object;
return $xml_object;
}
also you can use this function for loading array from xml , but i suggest you to use kris@h3x.com's functions[ http://www.devdump.com/phpxml.php ].
function xml_to_array($xml_tree,$root=false){
$array_name=$xml_tree['tag'];
foreach ($xml_tree['children'] as $children){
$child_id=$children['attributes']['id'];
$child_name=$children['tag'];
$child_name.=($child_id) ? "__".$child_id:'';
if (is_array($children['children'])) {
$child_array=xml_to_array($children);
$temp_array[$child_name]=$child_array;
} else {
$temp_array[$child_name]=$children['value'];
}
}
if (!$root)
$xml_array=$temp_array;
else
$xml_array[$array_name]=$temp_array;
return $xml_array;
}
bisqwit at iki dot fi dot not dot for dot ads dot invalid
20-Nov-2002 08:33
I made my own xml2tree()-function.
The difference to the previously suggested ones on this manual page is that this creates a tree where the tags are keys.
Example input:
<tree>
<leaf name="gruu"> pl </leaf>
<animal type="cat" />
</tree>
Example output:
Array
( 'tree' => Array
( 0 => Array
( 'children' => Array
( 'leaf' => Array
( 0 => Array
( 'attrs' => Array('name' => 'gruu'),
'values' => Array(0 => ' pl ')
) ),
'animal' => Array
( 0 => Array
( 'attrs' => Array('type' => 'cat')
) ) ) ) ) )
function xml2tree2($string)
{
$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, $string, $vals, $index);
xml_parser_free($parser);
$stack = array( array() );
$stacktop = 0;
$parent = array();
foreach($vals as $val)
{
$type = $val['type'];
if($type=='open' || $type=='complete')
{
// open tag
$stack[$stacktop++] = $tagi;
$tagi = array('tag' => $val['tag']);
if(isset($val['attributes'])) $tagi['attrs'] = $val['attributes'];
if(isset($val['value']))
$tagi['values'][] = $val['value'];
}
if($type=='complete' || $type=='close')
{
// finish tag
$tags[] = $oldtagi = $tagi;
$tagi = $stack[--$stacktop];
$oldtag = $oldtagi['tag'];
unset($oldtagi['tag']);
$tagi['children'][$oldtag][] = $oldtagi;
$parent = $tagi;
}
if($type=='cdata')
{
$tagi['values'][] = $val['value'];
}
}
return $parent['children'];
}
Ps: Note that the xml parsing for some reason chokes if the values contain <> (even inside ""). You need to escape them as < and >.
Same for & (for obvious reasons). Escape it as &.
For example " " should be written as "&nbsp;".
joeldegan at yahoo dot com
19-Nov-2002 04:40
Using one of the nice function above, I was able to get these two great structures below on nmap info for a machine and then the PID lists for each open port.. Very handy for my current project.
$test = `nmap -sU -sS localhost -oX -`;
$ret = xml2array($test);
$z=0;
while (list($key,$val)=each($ret["host"][0][3]["children"])){
if ($val["tag"] == "PORT"){
$new[$z]["PROTOCOL"] = $val["attributes"]["PROTOCOL"];
$new[$z]["PORT"] = $val["attributes"]["PORTID"];
//echo $val["attributes"]["PROTOCOL"]."\n";
//echo $val["attributes"]["PORTID"]."\n";
$z++;
}//fi
}//wend
print_r($new);
function port_pids($portnum){
$ret = `lsof -i :$portnum -F pc`;
$ret = explode("\n", $ret);
$z=0;
for($a=0;$a<count($ret);$a++){
$port = $ret[$a];
$a++;
$com = $ret[$a];
$new[$z]["PID"]=substr($port, 1);
$new[$z]["COMMAND"]=substr($com, 1);
$z++;
}//wend
return $new;
}//end function
print_r(port_pids("3306"));
no at chance dot com
08-Nov-2002 10:38
Thanks to gdemartini's tree algorithm , I was able to create a variation that display's the information in a way that allows you to then easily parse the information with less code and process time. Here's the variation and I've also posted a quick function at the bottom that allows you to visually display complex arrays in human readable format. Hope this helps someone as much as gdemartini's functions helped me.
I decided to post it on my server as opposed to the forums as it was too large and needed some good html formatting. It can found at http://www.devdump.com/phpxml.php .
ambi at gmx dot de
05-Oct-2002 03:59
Sorry.. unreachable and unneeded code in my posting above :-)
Here is the fixed version:
function xml_condense_tree($tree) {
if ($tree["children"]) {
unset($nodecounters);
foreach ($tree['children'] as $index => $node) {
if (!isset($nodecounters[$node['tag']]))
$nodecounters[$node['tag']]=1;
else $nodecounters[$node['tag']]++;
}
foreach ($tree['children'] as $index => $node) {
if (isset($node['children'])) {
$tree['children'][$index] = xml_condense_tree($node);
} elseif (isset($node['value']) and !$node['attributes'] and $nodecounters[$node['tag']]==1) {
$tree['values'][$node['tag']] = $node['value'];
unset($tree['children'][$index]);
}
}
}
if (!$tree['children']) unset($tree['children']);
return $tree;
}
09-Aug-2002 01:20
This is based on a comment above by mh@mainlab.de. This version makes more sense to me and I included a few comments that might make it more usefull for specific purposes.
// Look through $vals and $index,
// generated by parse_xml_data(),
// for $location. $location resembles a unix path.
// E.g. "my/xml/path/to/my/data".
// Note: This is NOT a full-feature xml utility.
// Your path must be unique in the xml data
// and the tags must not contain slashes.
//
// returns: cdata from last element of $location
// or NULL.
function get_xml_values($vals, $index, $location) {
$location = explode('/', $location);
$location_level = count($location);
for ($n=0; $n<$location_level; ++$n) {
$val_indexes=$index[$location[$n]];
$good=0;
for ($i=0; $i<count($val_indexes); ++$i) {
$v = $vals[$val_indexes[$i]];
// level is 1 based, n is 0 based:
if ($v['level']-1 == $n) {
if ($v['type'] == 'open') {
// We found an open tag at the correct level.
$good=1;
break; // Remove break to avoid 'open' hiding
// 'complete' at last $n. (Speed Vs. Robustness:
// For now, I'm going with speed).
}
if ($v['type'] == 'complete') {
// We found it, after finding all the prior elements:
// You may want to change this to
// return $v
// so you can call $v['value'] and $v['attributes']
return $v['value'];
}
}
}
if (!$good) {
return NULL;
}
}
}
farinspace-at-hotmail-dot-com
20-Jul-2002 06:41
Wow, took a while to consume all of this. I am a noobie at XML, in fact i just really started working with it today. I went ahead and added to the above code by gdemartini@bol.com.br. I also added the update by nyk@cowham.net.
Added 'value' => $vals[$i]['value'] to the open case, as some nodes have both cdata as well as a children node. In previous versions of the functions the cdata of a node(a) that also had children would get stored in the children's array of the parent node(a). Now only children values are in the children array and the cdata is in the value key of the parent node(a).
Added 'value' => $vals[$i]['value'] to tree array as the vals array produced by PHP includes a value to the top most node if no children exist (for completeness).
Also, using xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE,1) will strip all white space from both around the XML tags as well as inside the cdata. I added a piece of code by waldo@wh-e.com to take care of this. All SPACE characters are now only stripped from around the XML tags and cdata spacing is retained.
CODE:
***************************************
function GetChildren($vals, &$i) {
$children = array();
while (++$i < sizeof($vals)) {
// compair type
switch ($vals[$i]['type']) {
case 'cdata':
$children[] = $vals[$i]['value'];
break;
case 'complete':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value']
);
break;
case 'open':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value'],
'children' => GetChildren($vals, $i)
);
break;
case 'close':
return $children;
}
}
}
function GetXMLTree($file) {
$data = implode('', file($file));
// by: waldo@wh-e.com - trim space around tags not within
$data = eregi_replace(">"."[[:space:]]+"."<","><",$data);
// XML functions
$p = xml_parser_create();
// by: anony@mous.com - meets XML 1.0 specification
xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
xml_parse_into_struct($p, $data, &$vals, &$index);
xml_parser_free($p);
$i = 0;
$tree = array();
$tree[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => $vals[$i]['attributes'],
'value' => $vals[$i]['value'],
'children' => GetChildren($vals, $i)
);
return $tree;
}
***************************************
dave at ovumdesign dot com
03-Apr-2002 03:32
Here is another variation on gdemartini@bol.com.br's nice tree algorithm. I have included nyk@cowham.net's "cdata" suggestion. I made a few changes to suppress unitialized variable warnings with error_reporting(E_ALL), and made a few stylistic changes.
There are two functional modifications as well. The first is the added option to expat to turn off "case folding", since XML is technically a case-sensitive language. The second is the returned tree - instead of returning a single-element list (an array with index 0 pointing to the top node) like the original algorithm, it returns the top node itself. This saves you an unnecessary level of indirection.
function xml_get_children($vals, &$i) {
$children = array();
if (isset($vals[$i]['value'])) $children[] = $vals[$i]['value'];
while (++$i < count($vals)) {
switch ($vals[$i]['type']) {
case 'cdata':
$children[] = $vals[$i]['value'];
break;
case 'complete':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => isset($vals[$i]['attributes'])?
$vals[$i]['attributes'] : null,
'value' => $vals[$i]['value'],
);
break;
case 'open':
$children[] = array(
'tag' => $vals[$i]['tag'],
'attributes' => isset($vals[$i]['attributes'])?
$vals[$i]['attributes'] : null,
'children' => xml_get_children($vals, $i),
);
break;
case 'close':
return $children;
}
}
}
function xml_get_tree($file) {
$data = join('', file($file));
$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, $data, $vals, $index);
xml_parser_free($parser);
return array(
'tag' => $vals[0]['tag'],
'attributes' => isset($vals[0]['attributes'])?
$vals[0]['attributes'] : null,
'children' => xml_get_children($vals, $i = 0),
);
}
This function takes a tree returned by xml_get_tree() and simplifies it similarly to voncken@mailandnews.com and oknapp@soly.de's suggestions. It finds any nodes that expat considered "complete" (childless) and contain no attributes, and it moves the tag name (as the key) and value into an element in the parent node's "values" array.
For example:
$tree = xml_get_tree('moldb.xml');
$tree = xml_condense_tree($tree);
echo $tree['children'][1]['values']['name'];
Outputs:
Lysine
function xml_condense_tree($tree) {
foreach ($tree['children'] as $index => $node) {
if (isset($node['children'])) {
$tree['children'][$index] = xml_condense_tree($node);
} elseif (isset($node['value']) and !$node['attributes']) {
$tree['values'][$node['tag']] = $node['value'];
unset($tree['children'][$index]);
}
}
if (!$tree['children']) unset($tree['children']);
return $tree;
}
Enjoy,
Dave
voncken at mailandnews dot com
06-Jan-2002 05:24
I needed something a bit different:
this is the result of my xml2array function (from the moldb example)
Array
(
[molecule] => Array
(
[0] => Array
(
[name] => Alanine
[symbol] => ala
[code] => A
[type] => hydrophobic
)
[1] => Array
(
[name] => Lysine
[symbol] => lys
[code] => K
[type] => charged
)
)
)
function xml2array($data)
{
//mvo voncken@mailandnews.com
//original ripped from on the php-manual:gdemartini@bol.com.br
//to be used for data retrieval(result-structure is Data oriented)
$p = xml_parser_create();
xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($p, $data, &$vals, &$index);
xml_parser_free($p);
$tree = array();
$i = 0;
//array_push($tree, array('tag' => $vals[$i]['tag'], 'attributes'=> $vals[$i]['attributes'], 'children' => ));
$tree = GetChildren($vals, $i);
return $tree;
}
function GetChildren($vals, &$i)
{
$children = array();
if ($vals[$i]['value']) array_push($children, $vals[$i]['value']);
$prevtag = "";
while (++$i < count($vals)) // so pra nao botar while true ;-)
{
switch ($vals[$i]['type'])
{
case 'cdata':
array_push($children, $vals[$i]['value']);
break;
case 'complete':
$children[ strtolower($vals[$i]['tag']) ] = $vals[$i]['value'];
break;
case 'open':
//restartindex on unique tag-name
$j++;
if ($prevtag <> $vals[$i]['tag']) {
$j = 0;
$prevtag = $vals[$i]['tag'];
}
$children[ strtolower($vals[$i]['tag']) ][$j] = GetChildren($vals,$i);
break;
case 'close':
return $children;
}
}
}
| |