|
Тема |
JSP - XML SAX Parser |
|
Автор | Mитko (Нерегистриран) | |
Публикувано | 12.07.04 12:30 |
|
|
Ще съм благодарен ако някой превърне тази програма от PHP на JSP(по възможност без използване на XSLT). По-долу
са съответно XML кода(трябва да е във файл "fox.xml"), PHP кода и output-а от скрипта:
<?xml version="1.0"?>
<sentence>The <animal color="blue">fox</animal> leaped over the <vegetable
color="green">cabbage</vegetable> patch and vanished into the darkness.</sentence>
-----------------------------------------------------------------------
<html>
<head>
<basefont face="Arial">
</head>
<body>
<?php
// run when start tag is found
function startElementHandler($parser, $name, $attributes)
{
echo "Found opening tag of element: <b>$name</b> ";
// process attributes
while (list ($key, $value) = each ($attributes))
{
echo "Found attribute: <b>$key = $value</b> ";
}
}
// run when end tag is found
function endElementHandler($parser, $name)
{
echo "Found closing tag of element: <b>$name</b> ";
}
// run when cdata is found
function characterDataHandler($parser, $cdata)
{
echo "Found CDATA: <i>$cdata</i> ";
}
// XML data file
$xml_file = "fox.xml";
// initialize parser
$xml_parser = xml_parser_create();
// set callback functions
xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($xml_parser, "characterDataHandler");
// read XML file
if (!($fp = fopen($xml_file, "r")))
{
die("File I/O error: $xml_file");
}
// parse XML
while ($data = fread($fp, 4096))
{
// error handler
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die("XML parser error: " .
xml_error_string(xml_get_error_code($xml_parser)));
}
}
// all done, clean up!
xml_parser_free($xml_parser);
?>
</body>
</html>
----------------------------------------------------------------------
Found opening tag of element: SENTENCE
Found CDATA: The
Found opening tag of element: ANIMAL
Found attribute: COLOR = blue
Found CDATA: fox
Found closing tag of element: ANIMAL
Found CDATA: leaped over the
Found opening tag of element: VEGETABLE
Found attribute: COLOR = green
Found CDATA: cabbage
Found closing tag of element: VEGETABLE
Found CDATA: patch and vanished into the darkness.
Found closing tag of element: SENTENCE
|
| |
|
|
|