You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by karthikeyan <ka...@aspiresys.com> on 2001/09/06 19:49:51 UTC
JSP XML
----- Original Message -----
From: karthikeyan <ka...@aspiresys.com>
To: <xe...@xml.apache.org>
Sent: Thursday, September 06, 2001 8:37 PM
Subject: jsp xml
>
> Hi Guys,
>
> I am using the below class in my jsp file to read an xml and print only
> the necessary element value. I got this example from a book and it is
> working great but i feel for reading an element value from an xml this is
> really a huge code. I require your suggestions on how to make the Java
Code
> and the code in the JSP file smaller and effiecient. Any code changes or
> new codes are welcome.
>
> XML File
> -----------
>
> <?xml version='1.0'?>
> <SearchResult>
> <ResultHeader>
> <MemberID>12345</MemberID>
> <SearchType>quick</SearchType>
> <Result>success</Result>
> <CurrentPage>5</CurrentPage>
> <TotalPages>10</TotalPages>
> </ResultHeader>
> </SearchResult>
>
> Java File
> -----------
>
> package postcards;
>
> import java.io.*;
> import java.util.Hashtable;
> import org.xml.sax.*;
>
> public class SAXHandler extends HandlerBase {
> private Hashtable table = new Hashtable();
> private String currentElement = null;
> private String currentValue = null;
>
> public void setTable(Hashtable table) {
> this.table = table;
> }
>
> public Hashtable getTable() {
> return table;
> }
>
> public void startElement(String tag, AttributeList attrs)
> throws SAXException {
> currentElement = tag;
> }
>
> public void characters(char[] ch, int start, int length)
> throws SAXException {
> currentValue = new String(ch, start, length);
> }
>
> public void endElement(String name) throws SAXException {
> if(currentElement.equals(name)) {
> table.put(currentElement, currentValue);
> }
> }
> }
>
> JSP File
> ----------
>
> <%@ page import="java.io.*,
> java.util.Hashtable,
> org.w3c.dom.*,
> org.xml.sax.*,
> javax.xml.parsers.SAXParserFactory,
> javax.xml.parsers.SAXParser,
> postcards.SAXHandler" %>
> <%
> File file =
> new
>
File(getServletConfig().getServletContext().getRealPath("/xml/usersearch.xml
> "));
> file://out.println(file);
> FileReader reader = new FileReader(file);
> Parser parser;
> SAXParserFactory spf = SAXParserFactory.newInstance();
> SAXParser sp = spf.newSAXParser();
> SAXHandler handler = new SAXHandler();
> sp.parse(new InputSource(reader), handler);
> Hashtable cfgTable = handler.getTable();
> %>
> <html>
> <head>
> <title></title>
> <meta name='description' content=''>
> <meta name='keywords' content=''>
> </head>
> <body bgcolor='#ffffff' text='#000000' link='#0000FF' vlink='#800080'>
> Member ID : <%= (String)cfgTable.get(new String("MemberID")) %><br>
> TotalPages : <%= (String)cfgTable.get(new String("TotalPages")) %>
> </body>
> </html>
>
> Waiting for your earliest reply,
>
> With Regard,
>
> b.karthikeyan.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org