note 26836 added to ref.xml
| From: | mreilly at ZEROSPAM dot MAC dot COM | Date: | Thu, 14 Nov 2002 16:01:20 +0000 |
| Subject: | note 26836 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-39489@lists.php.net to get a copy of this message | ||
I wanted a way to reference the XML tree by path. I couldn't find exactly what I wanted, but
using examples here and on phpbuilder.com came up with this. This results in a nested associative
array, so elements can be accessed in the manner:
echo $ary_parsed_file['path']['to']['value'];
<?php
// Display the print_r() output in a readable format
echo '<PRE>';
// Array to store current xml path
$ary_path = array();
// Array to store parsed data
$ary_parsed_file = array();
// Starting level - Set to 0 to display all levels. Set to 1 or higher
// to skip a level that is common to all the fields.
$int_starting_level = 1;
// what are we parsing?
$xml_file = 'label.xml';
// declare the character set - UTF-8 is the default
$type = 'UTF-8';
// create our parser
$xml_parser = xml_parser_create($type);
// set some parser options
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
// this tells PHP what functions to call when it finds an element
// these funcitons also handle the element's attributes
xml_set_element_handler($xml_parser, 'startElement','endElement');
// this tells PHP what function to use on the character data
xml_set_character_data_handler($xml_parser, 'characterData');
if (!($fp = fopen($xml_file, 'r'))) {
die("Could not open $xml_file for parsing!\n");
}
// loop through the file and parse baby!
while ($data = fread($fp, 4096)) {
if (!($data = utf8_encode($data))) {
echo 'ERROR'."\n";
}
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf( "XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
// Display the array
print_r($ary_parsed_file);
// This function is called for every opening XML tag. We
// need to keep track of our path in the XML file, so we
// will use this function to add the tag name to an array
function startElement($parser, $name, $attrs=''){
// Make sure we can access the path array
global $ary_path;
// Push the tag into the array
array_push($ary_path, $name);
}
// This function is called for every closing XML tag. We
// need to keep track of our path in the XML file, so we
// will use this function to remove the last item of the array.
function endElement($parser, $name, $attrs=''){
// Make sure we can access the path array
global $ary_path;
// Push the tag into the array
array_pop($ary_path);
}
// This function is called for every data portion found between
// opening and closing tags. We will use it to insert values
// into the array.
function characterData($parser, $data){
// Make sure we can access the path and parsed file arrays
// and the starting level value
global $ary_parsed_file, $ary_path, $int_starting_level;
// Remove extra white space from the data (so we can tell if it's empty)
$str_trimmed_data = trim($data);
// Since this function gets called whether there is text data or not,
// we need to prevent it from being called when there is no text data
// or it overwrites previous legitimate data.
if (!empty($str_trimmed_data)) {
// Build the array definition string
$str_array_define = '$ary_parsed_file';
// Add a [''] and data for each level. (Starting level can be defined.)
for ($i = $int_starting_level; $i < count($ary_path); $i++) {
$str_array_define .= '[\'' . $ary_path[$i] . '\']';
}
// Add the value portion of the statement
$str_array_define .= " = '" . $str_trimmed_data . "';";
// Evaluate the statement we just created
eval($str_array_define);
// DEBUG
//echo "\n" . $str_array_define;
} // if
}
?>
--
http://www.php.net/manual/en/ref.xml.php
http://master.php.net/manage/user-notes.php?action=edit+26836
http://master.php.net/manage/user-notes.php?action=delete+26836
http://master.php.net/manage/user-notes.php?action=reject+26836