This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes xml parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xml parsing " Watch "xml parsing " New topic
Author

xml parsing

prithvi
Greenhorn

Joined: Aug 26, 2003
Posts: 14
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

Joined: Mar 22, 2000
Posts: 12758
    
    5
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

Joined: Aug 26, 2003
Posts: 14
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hello "prithvi",
I assume you didn't see my request in this thread. Please read it now.
Thanks.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Mar 24, 2004
Posts: 2
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: xml parsing
 
Similar Threads
parsing numeric & special characters
External DTD
XML Parsing - File Locked !
outOFMemoryException.
parsing comments with SAXParser from jdk1.4