You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Ashish Kulkarni <ku...@yahoo.com> on 2003/05/08 20:24:01 UTC
[OT] SAX paser code problem
Hi,
I am attching a simple code below to parse a file
using SAX and build a vector with data
But i am getting the Vector as blank from method
readXML(File file) why??
ashish
import java.io.*;
import java.util.*;
import org.xml.sax.*;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.*;
public class ReadExternalReportsXml extends
DefaultHandler
{
private Vector data = new Vector();
private boolean startElem = false;
private Vector data1 ;
public Vector readXML(File xmlFile) throws
SAXException, IOException,
ParserConfigurationException
{
// Use an instance of ourselves as the SAX event
handler
DefaultHandler handler = new
ReadExternalReportsXml();
// Use the default (non-validating) parser
SAXParserFactory factory =
SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(xmlFile, handler);
return data;
}
private void addData(Vector data1)
{
data.add(data1);
}
public static void main(String args[])throws
Exception
{
ReadExternalReportsXml rep = new
ReadExternalReportsXml();
System.out.println("vector is " + rep.readXML(new
File("C:\\mapsweb\\version6102\\WEB-INF\\externalreports.xml")));
}
public void startElement(String uri, String
localName, String qName, Attributes attributes) throws
org.xml.sax.SAXException
{
if(qName.equals("report"))
{
String name = attributes.getValue("name");
startElem = true;
data1 = new Vector();
data1.add(name);
}
}
public void endElement(String uri, String localName,
String qName) throws org.xml.sax.SAXException
{
addData(data1);
startElem = false;
}
public void characters(char[] parm1, int parm2, int
parm3) throws org.xml.sax.SAXException
{
if(startElem)
{
String value = new String(parm1, parm2, parm3);
data1.add(value);
}
}
public void endDocument() throws
org.xml.sax.SAXException
{
System.out.println("data " + totalData);
}
}
my XML file is
<?xml version='1.0' encoding='utf-8'?>
<reports>
<report name="test.imr">This is test
report</report>
<report name="test1.imr">This is test
report1</report>
<report name="test2.imr">This is test
report2</report>
<report name="test3.imr">This is test
report3</report>
</reports>
__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org