Hi,
I am using the following code (see below) from php.net
(http://www.php.net/manual/en/ref.xml.php, example 1) to parse an XML
file (encoded in UTF-8). I changed the code slightly so that the cdata
sections will be echoed an not the element names as in the original
example.
In the cdata sections of my XML file I have terms like this:
Cap<Finanzinstrument>
The parser echoes them as following (echo $data . "<br>";):
Cap
<
Finanzinstrument
Can anyone explain this to me? Why does the parser split the
cdata-section with < and >, in it? Is there any way to avoid
this?
Thanks very much in advance,
greetings, wenke
--------------------------------------------
<?php
$file = "ck_bsp.xml";
$depth = array();
function startElement($parser, $name, $attrs)
{
global $depth;
for ($i = 0; $i < $depth[$parser]; $i++) {
echo " ";
}
//echo "$name\n";
$depth[$parser]++;
}
function endElement($parser, $name)
{
global $depth;
$depth[$parser]--;
}
function characterData($parser, $data)
{
echo $data . "<br>";
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
?>
--------------------------------------------