Help with XML and PHP
| From: | John Levon | Date: | Mon, 03 Jul 2000 15:02:33 +0000 |
| Subject: | Help with XML and PHP | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-4506@lists.php.net to get a copy of this message | ||
I have perused the FAQ and docs in general, but couldn't find an answer.
I have an example lifted almost straight from the manual, which has been
tried under both 3.0 and 4.0. my code is :
<html>
<body>
<?
$file = "faq.xml";
$map_array = array(
"QUESTION" => "B",
"ANSWER" => "TT"
);
function startElement($parser, $name, $attrs) {
global $map_array;
if ($htmltag = $map_array[$name]) {
print "<$htmltag>";
}
}
function endElement($parser, $name) {
global $map_array;
if ($htmltag = $map_array[$name]) {
print "</$htmltag>";
}
}
function characterData($parser, $data) {
print $data;
}
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
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);
?>
</body>
and my test xml file is :
<?xml version='1.0'?>
<QUESTION>How muych ?</QUESTION>
<ANSWER>Too much</ANSWER>
but I get this :
How muych ?XML error: junk after document element at line 4
can anybody help ?
thanks
john
--
"Global Alert For All: Jesus Is Coming Soon !"
- Clarence L. Thomas IV on linux-activists