• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parsing XML Document with SAX

 
Ranganath .S.Junpal
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.coderanch.com/t/128074/XML/stop-parsing-file-SAX

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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic