Võ Văn Hải's blog

Chỉ có một điều tuyệt đối đó là mọi thứ đều tương đối…

Dùng SAX hiển thị tài liệu XML trên JSP

Tạo file SAXhandler.java có nội dung sau

package jaxp;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXhandler extends DefaultHandler{

private String html=“”;

@Override

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

throws SAXException {

String st=new String(ch,start,length).trim();

if(!st.equals(“”))

html+=“<td>”+st+“</td>”;

}

@Override

public void startDocument() throws SAXException {

html+=“<table border=’1′ width=’80%’>”;

html+=“<tr><td><b>TITLE</b></td><td><b>ARTIST</b></td><td><b>COUNTRY</b></td>”+

“<td><b>COMPANY</b></td><td><b>PRICE</b></td><td><b>YEAR</b></td></tr>”;

}

@Override

public void endDocument() throws SAXException {

html+=“</table>”;

}

@Override

public void startElement(String uri, String localName, String name,

Attributes attributes) throws SAXException {

if(name.equalsIgnoreCase(“cd”))

html+=“<tr>”;

}

@Override

public void endElement(String uri, String localName, String name)

throws SAXException {

if(name.equalsIgnoreCase(“cd”))

html+=“</tr>”;

}

public String getHtml() {

return html;

}

}

Tạo file index.jsp có nội dung sau

<?xml version=“1.0” encoding=“ISO-8859-1” ?>

<%@ page language=“java” contentType=“text/html; charset=ISO-8859-1”

pageEncoding=“ISO-8859-1”%>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;>

<%@page import=“javax.xml.parsers.SAXParserFactory”%>

<%@page import=“jaxp.SAXhandler”%>

<html xmlns=http://www.w3.org/1999/xhtml&#8221;>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1” />

<title>Working with XML form JSP</title>

</head>

<body>

<h1>Working with XML form JSP</h1>

<%

try {

String xmlfile=http://localhost:8080/XML_JSP/ex2/cdcatalog.xml&#8221;;

SAXParserFactory SPfactory=SAXParserFactory.newInstance();

javax.xml.parsers.SAXParser parser=SPfactory.newSAXParser();

SAXhandler hdl=new SAXhandler();

parser.parse(xmlfile,hdl);

out.println(hdl.getHtml());

} catch (Exception e) {

e.printStackTrace();

}

%>

</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ả sau khi hiển thị trên trình duyệt
https://vovanhai.files.wordpress.com/2008/09/sax01_jsp.png

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: