0 Replies Latest reply: Feb 13, 2010 8:26 AM by 843807 RSS

    Could not get the Data's while parsing XML file.

    843807
      Hi!
      Here i have created bean file using and contenthandler file to get the data from XML file and show it in table format using jsp.but when i run an jsp file it executes none in the file,what could be the error..could u anyone guide me..
      package saxbean;
      
      import java.io.*;
      import java.util.*;
      import org.xml.sax.*;
      import org.xml.sax.helpers.DefaultHandler;
      import javax.xml.parsers.SAXParserFactory;  
      import 
      javax.xml.parsers.ParserConfigurationException;
      import javax.xml.parsers.SAXParser;
      
      public class MySAXParserBean extends 
      DefaultHandler implements java.io.Serializable {
         private String text;
         private Vector vector = new Vector();
         private MyElement current = null;
      
         public MySAXParserBean() {
         }
      
         public Vector parse(String filename) throws 
         Exception {
           SAXParserFactory spf = 
            SAXParserFactory.newInstance(); 
           spf.setValidating(false);
           SAXParser saxParser = spf.newSAXParser(); 
           // create an XML reader
           XMLReader reader = saxParser.getXMLReader();
           FileReader file = new FileReader(filename);
           // set handler
           reader.setContentHandler(this);
           // call parse on an input source
           reader.parse(new InputSource(file));
           return vector;
         }
         
         // receive notification of the beginning of an 
         element 
         public void startElement (String uri, String name,
          String qName, Attributes atts) {
            current = new MyElement(
            uri, name, qName, atts);
            vector.addElement(current);
            text = new String();
      
         }
      
         // receive notification of the end of an element
         public void endElement (String uri, String name, 
         String qName) {
           if(current != null && text != null) {
              current.setValue(text.trim());
           }
           current = null;
         }
         
         // receive notification of character data
         public void characters (char ch[], int start, 
         int length) {
           if(current != null && text != null) {
              String value = new String(
              ch, start, length);
              text += value;
           }
         }
      }
      My bean file
      package saxbean;
      
      import org.xml.sax.Attributes;
      
      public class MyElement implements 
      java.io.Serializable {
         String uri;
         String localName;
         String qName;
         String value=null;
         Attributes attributes;
      
         public MyElement(String uri, String localName,
          String qName, Attributes attributes) {
            this.uri = uri;
            this.localName = localName;
            this.qName = qName;
            this.attributes = attributes;
         }
      
         public String getUri() {
            return uri;
         }
      
         public String getLocalName() {
            return localName;
         }
      
         public String getQname() {
            return qName;
         }
      
         public Attributes getAttributes() {
            return attributes;
         }
      
         public String getValue() {
            return value;
         }
      
         public void setValue(String value) {
            this.value = value;
         }
      }
      My Jsp file
      <html>
      <head>
      <title>sax parser</title>
      <%@ page import="java.util.*" %>
      <%@ page import="saxbean.*" %>
      </head>
      
      <body bgcolor="#ffffcc">
      
      <jsp:useBean id="saxparser" 
      class="saxbean.MySAXParserBean" />
      
      <%
      Collection stocks = 
      saxparser.parse("c:/stocks/stocks.xml");
      Iterator ir = stocks.iterator();
      %>
      
      <center>
      <h3>My Portfolio</h3>
      <table border="2" width="50%">
       <tr>
       <th>Stock Symbol</th>
       <th>Company Name</th>
       <th>Price</th>
       </tr>
       <tr>
      
      <%
      while(ir.hasNext()) {
        MyElement element = (MyElement) ir.next(); 
        String tag = element.getLocalName();
        if(tag.equals("symbol")) { %>
          <td><%= element.getValue() %></td>
        <% } else if (tag.equals("name")) { %>
          <td><%= element.getValue() %></td>
        <% } else if (tag.equals("price")) { %>
          <td><%= element.getValue() %><
          /td></tr><tr>
        <% } %>
      <% } %>
      
      </body>
      </html>
      but the output was only th tag had been executing in this program..