Dùng SAX hiển thị tài liệu XML trên JSP với JSPWriter
Tạo file MyHandler.java với nội dung sau
package jaxp;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler { private int stepCount, totalAge; private JspWriter out; private boolean insideAgeElement;
public MyHandler(JspWriter out) { this.out = out; }
public void startDocument() throws SAXException { try { out.write(++stepCount + “. Start of document<br>”); } catch (IOException e) { throw new SAXException(e); } } // end of startDocument()
public void endDocument() throws SAXException { try { out.write(++stepCount + “. End of document<p>”); out.write(“The total of all ages in the XML document is <b><i>” + totalAge + “</i></b>”); } catch (IOException e) { throw new SAXException(e); } } // end of endDocument()
public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException { if ( qName.equals(“age”)) { insideAgeElement = true; }
try { out.write(++stepCount + “. Start of element: <b>” + qName + “</b>”);
int numberOfAttributes = attrs.getLength();
if ( numberOfAttributes > 0 ) { out.write(“. Attributes: <ul>”); } // end of if () else out.write(“<br>”);
for ( int i=0; i<numberOfAttributes; i++) { out.write(“<li>” + attrs.getQName(i) + ” = “ + attrs.getValue(i) + “</li>”); } // end of for ()
if ( numberOfAttributes > 0 ) { out.write(“</ul>”); } } catch (IOException e) { throw new SAXException(e); } } // end of startElement()
public void endElement(String namespaceURI, String localName, String qName) throws SAXException { if ( qName.equals(“age”) ) { insideAgeElement = false; }
try { out.write(++stepCount + “. End of element <b>” + qName + “</b><br>”); } catch (IOException e) { throw new SAXException(e); } // end of try-catch
} // end of endElement()
public void characters(char[] chars, int start, int length) throws SAXException { String content = new String(chars, start, length);
if ( insideAgeElement ) { int age = Integer.parseInt(content); totalAge += age; }
try { out.write(++stepCount + “. Character content = “);
if ( length > 0 ) out.write(“<b>” + content + “</b><br>”); } catch (IOException e) { throw new SAXException(e); } // end of try-catch
} // end of characters() } // end of class MyHandler
|
Tạo file index.jsp có nội dung sau
<%@page import=“jaxp.MyHandler”%> <%@page import=“javax.xml.parsers.SAXParserFactory”%> <%@page import=“javax.xml.parsers.SAXParser”%> <html> <head><title>JSP and SAX</title></head> <body> <% SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); MyHandler myHandler = new MyHandler(out);
parser.parse(“http://localhost:8080/XML_JSP/ex3/cdcatalog.xml”, myHandler); %> </body> </html> |
File dữ liệu cdcatalog.xml ở đây có dạng
<catalog> − <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> − <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd>… |
Kết quả hiển thị trên trình duyệt
Leave a comment