Click to See Complete Forum and Search --> : Parsing an XML document with PHP


brohrbach
04-10-2006, 11:10 AM
I'm trying to parse an XML document that contains an & in the data. When php parses the & it breaks the line into 3 lines instead of one.

Here's the PHP code:
function opening_element($parser, $element, $attributes){
global $flag1;
if ($element == "image"){
$flag1 = "image";
}
}
function closing_element($parser, $element){
global $flag1;
if ($element == "image"){
$flag1 = "";
}
}
function character_data($parser, $data){
global $flag1;
if ($flag1 == "image"){
global $images1;
$images1[] = $data;
}
}

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($parser, "opening_element", "closing_element");
xml_set_character_data_handler($parser, "character_data");

$document = file("exp1.xml");
foreach ($document as $line) {
xml_parse($parser, $line);
}
xml_parser_free($parser);


Here's the XML:
<image>img/exp/03/B-Architecture &amp; nature...jpg</image>

Here's the result:
img/exp/03/B-Architecture
&
nature...jpg

It breaks it into 3 lines when I want it in one line.

Any suggestions?

NogDog
04-11-2006, 08:00 AM
This appears to be a bug in some people's minds, and an "undocumented feature" in others' (including the PHP developers). For instance, see this bug report (http://bugs.php.net/bug.php?id=23631). You might want to look at this solution (http://us3.php.net/manual/en/function.xml-set-character-data-handler.php#49943) (flobee, 12-Feb-2005 10:53).

brohrbach
04-11-2006, 11:26 AM
Thanks for the reply. I was afraid I'd have to take care of the & myself. I was hoping there was a parameter or flag I could set to force the parser to figure it out but it doesn't look like it.