note 52567 added to function.xml-parse
| From: | bbellwfu at gmail dot com | Date: | Thu, 05 May 2005 21:51:28 +0000 |
| Subject: | note 52567 added to function.xml-parse | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88982@lists.php.net to get a copy of this message | ||
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>
----
Manual Page -- http://www.php.net/manual/en/function.xml-parse.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52567
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52567&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52567&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52567&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52567&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52567&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52567&report=yes
Search -- http://master.php.net/manage/user-notes.php