rocket mass heater kickstarter
The moose likes XML and Related Technologies and the fly likes Parsing XML Document with SAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Parsing XML Document with SAX" Watch "Parsing XML Document with SAX" New topic

Parsing XML Document with SAX

Ranganath .S.Junpal
Ranch Hand

Joined: Jan 16, 2005
Posts: 52
Hi folks,

I am building an application which will parse the xml document and fetch the value using the get/set accessors and the data is given to the Data Access Layer(DAL). The DAL then updates this data to the Database.

I have used the Sax parsers. I have crimson.jar and JAXP in my project.

The tags i am usign is as follows

But my program is not stable. when it parses it will give the values of the first 2 documents properly but after that it wont give the value of the <correctAns></correctAns> tags.

I will put my Actual code plz let me know whts wrong.. and plz let me know the Solution .

My XML Document(sample.xml)
<?xml version="1.0" encoding="UTF-8"?>
<Question>What is the full form of XML and who invented it?</Question>
<OptionOne>Extensible Markup Language</OptionOne>
<OptionTwo>Extemded Mark Language</OptionTwo>
<OptionThree>Mark Extender invented it!!!123456</OptionThree>
<OptionFour>None of these</OptionFour>

<Question>who is the founder of Microsoft</Question>
<OptionOne>Bill Johnson</OptionOne>
<OptionTwo>gates ballmer Language</OptionTwo>
<OptionFour>George Bill</OptionFour>
<CorrectAns>Bill Gates</CorrectAns>

<Question>whos the fournder of java</Question>
<OptionTwo>gates ballmer Language</OptionTwo>
<OptionFour>James Gosling</OptionFour>
<CorrectAns>James Gosling</CorrectAns>


The Parser code is as follows:-
package com.Mphasis.olrt.XMLParser;

import java.util.Hashtable;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import com.Mphasis.olrt.valueobject.QuestionVO;
import com.Mphasis.olrt.valueobject.XQuestionVO;
import com.Mphasis.olrt.dao.QuestionDAO.XmlDAO;

* @author Ranganath.S
* MCA , ICFAI National College.
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
public class XMLParserVO extends DefaultHandler
private IXmlState state=null;
private QuestionVO format=null;
private Hashtable prefixes=null;
public String sElement;
XQuestionVO list;
XQuestionVO dbase;
ArrayList al;
private ArrayList ai,bi;
XmlDAO Qdb;

public XMLParserVO()
prefixes=new Hashtable();
format=new QuestionVO();
dbase=new XQuestionVO();
dbase=new XQuestionVO();
list=new XQuestionVO();
System.out.println("inside the parser");

public void startDocument() throws SAXException
System.out.println("Start Document");

public void endDocument() throws SAXException
System.out.println("end Document");

public void startElement(String namespaceURI,String localName,String qName, Attributes atts) throws SAXException
System.out.println("inside startElement: "+qName);

public void endElement(String namespaceURI,String localName,String qName)throws SAXException
System.out.println("inside endelement: "+qName);

public void startPrefixMapping(String prefix,String uri) throws SAXException
System.out.println("Begin namespace prefix: "+prefix);

public void characters(char[] chars,int start,int length) throws SAXException
String charString =new String(chars,start,length);
System.out.println("Characters: "+charString);

else if(sElement.equalsIgnoreCase("OptionOne"))
//System.out.println(dbase.getOpt1()+"yo man!!!");
else if(sElement.equalsIgnoreCase("OptionTwo"))
//System.out.println(dbase.getOpt2()+"coyao man!!!");
else if(sElement.equalsIgnoreCase("OptionThree"))
//System.out.println(dbase.getOpt3()+"coyao man!!!");
else if(sElement.equalsIgnoreCase("OptionFour"))
//System.out.println(dbase.getOpt4()+"fpiroyao man!!!");
else if(sElement.equalsIgnoreCase("CorrectAns"))
//System.out.println(dbase.getCorrect_ans()+"fpiroyao man!!!");

public void changeState(IXmlState state)
System.out.println("State Changing");

public void warning(SAXParseException e) throws SAXException

public void fatalError(SAXParseException ex) throws SAXException

public void error(SAXParseException spe) throws SAXException

public void setObj(XQuestionVO al)
System.out.println("inside setObj "+ai.size());


public XQuestionVO getObj()
System.out.println("inside getSetObj "+bi.size());
return list;

please let me know the solution at the earliest.
Thank you
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
The code is a bit hard to understand, since much is happening in other classes. Once thing that looks possibly incorrect is the characters method. It seems to assume that all characters inside of an element will be reported in one call. If you study the javadocs for that method, you'll see that that is not the case - the method may be called several times.

In the future, if you post code of any length, please use code tags to keep it readable. They're accessible through the "CODE" button underneath the text entry field when posting.
[ January 15, 2006: Message edited by: Ulf Dittmer ]
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72

Hey take a look at this topic
I did something similar with SAX and there is code that will show you how to implement charchters() correctly

Hope it helps!
I agree. Here's the link:
subject: Parsing XML Document with SAX