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
<Question></Question>
<op1></op1>
<op2></op2>
<op3></op3>
<op4></op4>
<correctAns></correctAns>
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"?>
<QuestionBase>
<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>
<CorrectAns>Praneeth</CorrectAns>
<Question>who is the founder of Microsoft</Question>
<OptionOne>Bill Johnson</OptionOne>
<OptionTwo>gates ballmer Language</OptionTwo>
<OptionThree>Billgates</OptionThree>
<OptionFour>George Bill</OptionFour>
<CorrectAns>Bill Gates</CorrectAns>
<Question>whos the fournder of
java</Question>
<OptionOne>Bill</OptionOne>
<OptionTwo>gates ballmer Language</OptionTwo>
<OptionThree>Billgates</OptionThree>
<OptionFour>James Gosling</OptionFour>
<CorrectAns>James Gosling</CorrectAns>
</QuestionBase>
---------------------------------------------------------------------------
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()
{
state=InitialState.getInstance();
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);
sElement=qName;
state.handleStartElement(localName,atts,this,format);
}
public void endElement(String namespaceURI,String localName,String qName)throws SAXException
{
if(sElement.equalsIgnoreCase("CorrectAns"))
{
setObj(dbase);
}
System.out.println("inside endelement: "+qName);
state.handleEndElement(localName,this,format);
}
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);
state.handleCharacterData(charString,format);
if(sElement.equalsIgnoreCase("Question"))
{
dbase.setQuestion(charString);
}
else if(sElement.equalsIgnoreCase("OptionOne"))
{
dbase.setOpt1(charString);
//System.out.println(dbase.getOpt1()+"yo man!!!");
}
else if(sElement.equalsIgnoreCase("OptionTwo"))
{
dbase.setOpt2(charString);
//System.out.println(dbase.getOpt2()+"coyao man!!!");
}
else if(sElement.equalsIgnoreCase("OptionThree"))
{
dbase.setOpt3(charString);
//System.out.println(dbase.getOpt3()+"coyao man!!!");
}
else if(sElement.equalsIgnoreCase("OptionFour"))
{
dbase.setOpt4(charString);
//System.out.println(dbase.getOpt4()+"fpiroyao man!!!");
}
else if(sElement.equalsIgnoreCase("CorrectAns"))
{
dbase.setCorrect_ans(charString);
//System.out.println(dbase.getCorrect_ans()+"fpiroyao man!!!");
}
}
public void changeState(IXmlState state)
{
System.out.println("State Changing");
this.state=state;
}
public void warning(SAXParseException e) throws SAXException
{
e.printStackTrace();
}
public void fatalError(SAXParseException ex) throws SAXException
{
ex.printStackTrace();
}
public void error(SAXParseException spe) throws SAXException
{
System.err.print(spe.getMessage());
}
public void setObj(XQuestionVO al)
{
ai=al.getQuestion();
System.out.println("inside setObj "+ai.size());
list=al;
}
public XQuestionVO getObj()
{
bi=list.getQuestion();
System.out.println("inside getSetObj "+bi.size());
return list;
}
}
---------------------------------------------------------------------------
please let me know the solution at the earliest.
Thank you