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.
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
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
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)?
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!
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!