note 94372 added to example.xml-structure
| From: | roan at horning dot us | Date: | Sat, 31 Oct 2009 05:07:03 +0000 |
| Subject: | note 94372 added to example.xml-structure | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162299@lists.php.net to get a copy of this message | ||
The following example will echo back an xml file using the basic event based parser. Currently, (at
least in the version of PHP5 I'm using) it will skip The XML declaration and the Doctype
declaration--they don't seem to be captured by the default handler.
<?php
echo "<pre>";
$file = "test.xml";
echo $file."\n";
global $inTag;
$inTag = "";
$xml_parser = xml_parser_create_ns();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_processing_instruction_handler($xml_parser, "pi_handler");
xml_set_default_handler($xml_parser, "parseDEFAULT");
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "contents");
if (!($fp = fopen($file, "r"))) {
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)));
}
}
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);
function startElement($parser, $name, $attrs) {
global $inTag;
global $depth;
$padTag = str_repeat(str_pad(" ", 3), $depth);
if (!($inTag == "")) {
echo ">";
}
echo "\n$padTag<$name";
foreach ($attrs as $key => $value) {
echo "\n$padTag".str_pad(" ", 3);
echo " $key=\"$value\"";
}
$inTag = $name;
$depth++;
}
function endElement($parser, $name) {
global $depth;
global $inTag;
global $closeTag;
$depth--;
if ($closeTag == TRUE) {
echo "</$name>";
$inTag = "";
} elseif ($inTag == $name) {
echo " />";
$inTag = "";
} else {
$padTag = str_repeat(str_pad(" ", 3), $depth);
echo "\n$padTag</$name>";
}
}
function contents($parser, $data) {
global $closeTag;
$data = preg_replace("/^\s+/", "", $data);
$data = preg_replace("/\s+$/", "", $data);
if (!($data == "")) {
echo ">$data";
$closeTag = TRUE;
} else {
$closeTag = FALSE;
}
}
function parseDEFAULT($parser, $data) {
$data = preg_replace("/</", "<", $data);
$data = preg_replace("/>/", ">", $data);
echo $data;
}
function pi_handler($parser, $target, $data) {
echo "<?$target $data?>\n";
}
echo "</pre>";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 96.234.191.84
----
Manual Page -- http://www.php.net/manual/en/example.xml-structure.php
Edit -- https://master.php.net/note/edit/94372
Del: integrated -- https://master.php.net/note/delete/94372/integrated
Del: useless -- https://master.php.net/note/delete/94372/useless
Del: bad code -- https://master.php.net/note/delete/94372/bad+code
Del: spam -- https://master.php.net/note/delete/94372/spam
Del: non-english -- https://master.php.net/note/delete/94372/non-english
Del: in docs -- https://master.php.net/note/delete/94372/in+docs
Del: other reasons-- https://master.php.net/note/delete/94372
Reject -- https://master.php.net/note/reject/94372
Search -- https://master.php.net/manage/user-notes.php