• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using Hibernate to Read XML

 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am intending to read an XML in the following format:

<Record>
<id></id>
<date></date>
....
....
</Record>
<Record>
<id></id>
<date></date>
....
....
</Record>

Now i want to read this XML pewr record and create a JAVA object and persist that object in DB.(Using SAX for parsing)

The problem i am facing is if there are any null values in any nodes i.e. date=null, then exception is thrown in parsing and program fails.I have captured this exception but i am not able to parse the further records.

So can anyone suggest the solution for this? Can i use hibernate to convert this XML to java objects and then check each JAVA objects for null values.

Any replies would be appreciated.
Saurabh
 
Mohd. Yakub
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
try to store Date="" or Date="null" string value .
with above condtion u can use jakarta's commons betwixt to read xml and map to java object and persist into DB.
http://jakarta.apache.org/commons/betwixt/
good luck,
yakub
[ December 06, 2005: Message edited by: Mohd. Yakub ]
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohd. Yakub:
Hi,
try to store Date="" or Date="null" string value .
with above condtion u can use jakarta's commons betwixt to read xml and map to java object and persist into DB.
http://jakarta.apache.org/commons/betwixt/
good luck,
yakub

[ December 06, 2005: Message edited by: Mohd. Yakub ]


I get this XML from 3rd party, so if i get a null, i cant help it.I can simply eat up the NullPointerException which i get while parsing.

My question is can hibernate or any other pasing mechanism will allow reading XML and if it has null values, simply skipping that node and read other nodes.

Saurabh
 
Mohd. Yakub
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
plz send the piece of code where u actually reading the xml file,
might be try-catch block had not placed appriopriately,i mean to say
plz check whether complete iteration block for getting values is within
the try-catch or specifically around that instruction only where u actually
retriving the value from the node
regards,
yakub
[ December 06, 2005: Message edited by: Mohd. Yakub ]
 
Saurabh Agrawal
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mohd. Yakub:
hi,
plz send the piece of code where u actually reading the xml file,
might be try-catch block had not placed appriopriately,i mean to say
plz check whether complete iteration block for getting values is within
the try-catch or specifically around that instruction only where u actually
retriving the value from the node
regards,
yakub

[ December 06, 2005: Message edited by: Mohd. Yakub ]



SAXParserFactory factory = SAXParserFactory.newInstance();
if (factory == null) {
String errorMessage = " :: processTBOFile created null SAXParserFactory. :: ";
throw new BatchException(errorMessage);
}

factory.setValidating(true);

// Use an instance of this class as the SAX event handler
TBOTBSReader handler = new TBOTBSReader(toSettleProcessor);
try {
// Parse the file
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(tboToSettleFile, handler);
} catch (SAXException se) {
// Error generated by this application
// (or a parser-initialisation error)
String errorMessage = ":: processTBOFile caught exception, Details :: ";
throw new BatchException(errorMessage + se.getMessage(), se);
}

This is the sample code.As youo can see method parse() internally calls startElement(),characters(),endElement() for processing nodes and its data.When a null value is encounterted in any node, it throws NullpointerException which is thrown as SAXException.

Saurabh
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can i use hibernate to convert this XML to java objects and then check each JAVA objects for null values.

Assuming you are using Hibernate3 you can treat XML as if it were another source of data. You don't need to explicitly parse the XML and create POJO's, you can just use the XML in place of a POJO. See the documentation to find out how.
 
Mohd. Yakub
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As youo can see method parse() internally calls startElement(),characters(),endElement() for processing nodes and its data.When a null value is encounterted in any node, it throws NullpointerException which is thrown as SAXException.

catch SAXException/NullpointerException within the characters() method ,do not throw from there to the upper callbacks ,so that further processing should not stop from there on encountering exception....
some inteligent work of putting exception block only at the affected set of instructions & catching application exception there only,logging it,as it should not stop the further processing
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic