Re: [xml] character data
| From: | Raditha Dissanayake | Date: | Sat, 11 Oct 2003 13:55:40 +0000 |
| Subject: | Re: [xml] character data | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-165848@lists.php.net to get a copy of this message | ||
What Tom has to say + you should note that character data may be called more than once for each call to startElement.
Tom Rogers wrote:
Hi, Saturday, October 11, 2003, 6:26:01 AM, you wrote: DA> I do not understand why this line does not work : DA> $info[$element] = $content; DA> but yet this works: echo $content; DA> why? what is the trick? DA> -- DA> $xml_comment_file = basename($svg_file, '.svg.xml') .'.info.xml'; DA> if (file_exists($xml_comment_file)) {-- Raditha Dissanayake. ------------------------------------------------------------------------ http://www.radinks.com/sftp/ | http://www.raditha/megaupload/ Lean and mean Secure FTP applet with | Mega Upload - PHP file uploader Graphical User Inteface. Just 150 KB | with progress bar.DA> $file = $xml_comment_file;DA> $info = array(); DA> $element = null;DA> function startElement($parser, $name, $attrs) { DA> global $element; DA> $element = $name; DA> } DA> function endElement($parser, $name) { DA> print ""; DA> } DA> function characterData($parser, $content) { DA> global $info; DA> global $element; DA> $info[$element] = $content; DA> //$info[$element] = sprintf("%s", $content); DA> }DA> $xml_parser = xml_parser_create(); DA> xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, FALSE); DA> xml_set_element_handler($xml_parser, "startElement", "endElement"); DA> xml_set_character_data_handler($xml_parser, "characterData");DA> if (!($fp = fopen($file, 'r'))) DA> die("could not open XML input\n");DA> while ($data = fread($fp, 4096)) DA> if (!xml_parse($xml_parser, $data, feof($fp))) DA> die(sprintf("XML error: %s at line %d", DA> xml_error_string(xml_get_error_code($xml_parser)), DA> xml_get_current_line_number($xml_parser)));DA> xml_parser_free($xml_parser);DA> print_r($info);DA> } DA> /* DA> # the output is without $content : DA> Array DA> (DA> [info] => DA> [file] => DA> [orig-file] => DA> [orig-author] => DA> [bitmap-src] => DA> [orig-date] => DA> [svg-date] =>DA> ) DA> */ the function characterData can be called with whitespace which is probably overwriting your content try this $content = trim($content); if(!empty($content)) $info[$element] = $content;