• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xml parsing

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
 
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic