JSP - XML SAX Parser

4 отговора 172 прегледа
#1

JSP - XML SAX Parser

Ще съм благодарен ако някой превърне тази програма от 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

Отговор
в отговор на Mитko #2

Re: JSP - XML SAX Parser

Eto neshto za nachalo:

<%@ page import="java.io.*" %>
<%@ page import="org.jdom.*" %>
<%@ page import="org.jdom.input.*"%>
<%@ page import="org.jdom.output.*"%>

<%
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(request.getParameter("file")));
XMLOutputter fmt = new XMLOutputter();
out.println(fmt.outputString(doc));
}
catch (Exception ex) {
System.out.println(ex.toString());
}
%>

Tova neshto mislj che shte ti svurshi rabota malko trjbva samo da go poobrabotish.

Отговор
в отговор на SrlClr #3

Re: JSP - XML SAX Parser

Благодаря SrlClr,
не знам защо обаче тези пакети ми липсват w J2EE:
"org.jdom.*","org.jdom.input.*","org.jdom.output.*" и затова не мога да пробвам твоя пример.

Отговор
в отговор на Mитko #4

Re: JSP - XML SAX Parser

След като си поиграх доста успях да си я направя програмката(копие на PHP скрипта),
работи с J2EE. Слагам я за всеки случай ако на някой му свърши работа:


---------------------------------------------------------------------------------

<%@ page import="org.xml.sax.Attributes,org.xml.sax.SAXException,org.xml.sax.helpers.DefaultHandler,javax.xml.parsers.SAXParserFactory,javax.xml.parsers.SAXParser,java.io.File" %>

<%!
String sOutput;


public class ProductEventHandler
extends DefaultHandler {
// event handlers could go here


public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {

sOutput += "Found opening tag of element: <b>" + qName + "</b>
";

for(int i = 0; i < attributes.getLength(); i++)
{
String attrName = attributes.getQName(i);
sOutput += "Found attribute: <b>" + attrName + " = " + attributes.getValue(attrName) + "</b>
";

}

}


public void endElement(String uri, String localName, String qName)
throws SAXException {

sOutput += "Found closing tag of element: <b>" + qName + "</b>
";

}


public void characters(char[] ch, int start, int length)
throws SAXException {

sOutput += "Found CDATA: <i>" + new String(ch, start, length) + "</i>
";

}


}
%>

<%
ProductEventHandler handler = new ProductEventHandler();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
File ourExample = new File("C:\\jakarta-tomcat-5.0.25\\webapps\\ROOT\\fox.xml");

sOutput = "";
parser.parse( ourExample, handler);
out.println(sOutput);

} catch (Exception e) {
out.println(e.getMessage());
}
%>

---------------------------------------------------------------------------------

Отговор
в отговор на Mитko #5

Re: JSP - XML SAX Parser

Sorry, zabravih da ti kaja che trjbva da svalish jdom API -to ot nego idvat tezi paketi.

Отговор