• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xml parsing

 
prithvi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to parse an xml file. I am parsing it using dom. The problem i am facing is : When there are some optional fields which means that the xml file may or may not contain them my parser should not read them.
So how do i stop my parser to reading the optional fields.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A DOM parser is going to read and parse the whole document - you can't pick and choose. It is up to your subsequent code to handle only the elements you want. Why is this a problem?
Bill
 
prithvi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly sorry to say that the parser i am using is Sax....The problem is that i shud read information from the xml and store it in the db. when i say a field is optional user can have or not have that tag in the xml.So if i am reading by tag i am getting the exceptions. Firstly is the approach of using sax and reading xml is ok or plz let me know the right way
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "prithvi",
I assume you didn't see my request in this thread. Please read it now.
Thanks.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prithvi:
when i say a field is optional user can have or not have that tag in the xml.So if i am reading by tag i am getting the exceptions.
The only way to do this is to simply implement the necessary logic in your SAX handler code. Why are you getting exceptions (and what exceptions)?
 
R Vaidya
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prithvi,

I am not sure if u r using a schema to validate the xml file. If so make the element an optional one so that u may not get an exception while parsing. Something like this :
<xs: element name="abc" type="xs: string" minOccurs="0" />
This may help!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Vaidya,
I'll have to ask you as well to edit your display name a bit so that it complies with our naming policy.
Thanks, and welcome to the JavaRanch!
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic