Parser parser = ParserFactory.makeParser( .... );
Parser parser = ParserFactory.makeParser ("org.apache.xerces.parsers.SAXParser");
DocumentHandler handler = new SpitElements ();
public class SpitElements extends HandlerBase {
public void startElement (String name, AttributeList atts) throws SAXException
{
System.out.println("Start element: " + name);
}
public void endElement (String name) throws SAXException
{
System.out.println("End element: " + name);
}
public void characters(char[] charArray, int start, int length)
throws SAXException {
String content = new String(charArray, start, length);
System.out.println(" Contents: " + content);
}
public class SpitElements2 implements DocumentHandler {
public void setDocumentLocator (Locator locator) { }
public void startDocument() throws SAXException {
System.out.println("--------- START"); }
public void endDocument() throws SAXException {
System.out.println("--------- END");
}
public void startElement (String name, AttributeList atts)
throws SAXException {
System.out.println("Start element: " + name); }
public void endElement (String name) throws SAXException {
System.out.println("End element: " + name); }
public void characters (char[] charArray, int start, int length)
throws SAXException {
String content = new String(charArray, start, length);
System.out.println(" Contents: " + content);
}
public void ignorableWhitespace (char[] text, int start, int length)
throws SAXException { // do nothing here
}
public void processingInstruction(String target, String data) {}
}
Ce servlet est basé sur le code des exemples précédents
public class FilterFormContServlet2 extends HttpServlet {
private class MyDocumentHandler implements DocumentHandler {
public void setDocumentLocator(Locator locator) {}
.... }
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
// create a handler
Parser parser = ParserFactory.makeParser (......) ;
DocumentHandler handler = new MyDocumentHandler ();
parser.setDocumentHandler(handler);
parser.parse(xmlURL);
}
public void printTail () {
out.println("</pre>");
out.println("</body>");
out.println("</html>");
out.flush();
out.close();
}
FilterFormContServlet2$MyDocumentHandler.class
FilterFormContServlet2.class
FilterFormContServlet2.java
public class MySaxHandler extends HandlerBase {
// on ne se fatigue pas trop ici :=)
String current;
public void startElement (String name, AttributeList atts)
throws SAXException
{
if (name.equals("Title"))
out.println("<hr><h2>");
else if (name.equals("Entry")||name.equals("List")) { }
else out.println ("<dl><dt>" + name + "<dt>");
current = name;
}
public void characters(char[] charArray, int start, int length)
throws SAXException {
String content = new String(charArray, start, length);
if (current.equals("Title"))
out.println(content);
else out.println ("<dd>" + content + "<dd>");
}
public void endElement (String name)
{
if (name.equals("Title"))
out.println("</h2>");
else if (name.equals("Entry")||name.equals("List")) { }
else out.println ("</dl>");
out.println("<!-- end" + name + "-->" );
}
}