note 66487 added to function.xml-parse-into-struct
| From: | efredricksen at gmail dot com | Date: | Wed, 24 May 2006 07:55:38 +0000 |
| Subject: | note 66487 added to function.xml-parse-into-struct | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111913@lists.php.net to get a copy of this message | ||
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(); // the currently filling [child] XmlElement 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") { // push
$elements[$index]->children = array();
$stack[count($stack)] = &$elements;
$elements = &$elements[$index]->children;
}
}
if ($tag['type'] == "close") { // pop
$elements = &$stack[count($stack) - 1];
unset($stack[count($stack) - 1]);
}
}
return $elements[0]; // the single top-level element
}
// For example:
$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] =>
)
)
)
)
)
----
Server IP: 64.71.164.2
Probable Submitter: 63.224.61.138
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php
Edit -- http://master.php.net/note/edit/66487
Del: integrated -- http://master.php.net/note/delete/66487/integrated
Del: useless -- http://master.php.net/note/delete/66487/useless
Del: bad code -- http://master.php.net/note/delete/66487/bad+code
Del: spam -- http://master.php.net/note/delete/66487/spam
Del: non-english -- http://master.php.net/note/delete/66487/non-english
Del: in docs -- http://master.php.net/note/delete/66487/in+docs
Del: other reasons-- http://master.php.net/note/delete/66487
Reject -- http://master.php.net/note/reject/66487
Search -- http://master.php.net/manage/user-notes.php