note 62804 added to ref.xml
| From: | francisatmichaellewismusicdotcom at osu1 dot php dot net | Date: | Thu, 09 Mar 2006 23:20:42 +0000 |
| Subject: | note 62804 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-105172@lists.php.net to get a copy of this message | ||
I'm terrible at parsing XML, though I'm trying to parse some attributes, and access them
only. This is the only piece of code that I have found so far that enables you to select a certain
part of the xml file and display it. But I can't get it to display attributes. Any help on this
would be great, Thanks.
Here it the code I'm using. This code was posted near the bottom of this page.
<?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
}
?>
----
Server IP: 66.163.161.117
Probable Submitter: 24.159.49.153
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/note/edit/62804
Del: integrated -- http://master.php.net/note/delete/62804/integrated
Del: useless -- http://master.php.net/note/delete/62804/useless
Del: bad code -- http://master.php.net/note/delete/62804/bad+code
Del: spam -- http://master.php.net/note/delete/62804/spam
Del: non-english -- http://master.php.net/note/delete/62804/non-english
Del: in docs -- http://master.php.net/note/delete/62804/in+docs
Del: other reasons-- http://master.php.net/note/delete/62804
Reject -- http://master.php.net/note/reject/62804
Search -- http://master.php.net/manage/user-notes.php