note 60485 added to function.xml-parse-into-struct
| From: | xademax at gmail dot com | Date: | Sun, 08 Jan 2006 20:15:45 +0000 |
| Subject: | note 60485 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101419@lists.php.net to get a copy of this message | ||
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] =>
)
)
)
)
)
)
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60485
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60485&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60485&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60485&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60485&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60485&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60485&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60485&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60485&report=yes
Search -- http://master.php.net/manage/user-notes.php