File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes help needed with sax parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "help needed with sax parser" Watch "help needed with sax parser" New topic

help needed with sax parser

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144

Can someone please help me as to how can I parse the below xml file where the child nodes has the same name as the parent node:

<BAY_NO><![CDATA[BAY 392500]]></BAY_NO>
<BAY_NO><![CDATA[BAY 408506]]></BAY_NO>
<BAY_NO><![CDATA[BAY 408761]]></BAY_NO>
<BAY_NO><![CDATA[BAY 579602]]></BAY_NO>
<MOL_FORMULA><![CDATA[((C19 H29 N3 O)c(Zn)c)mix]]></MOL_FORMULA>

Currently my code works for the xml files like the one below where the parent node name is different than the child node names:


The java code is pasted below:

The tableName variable contains the parent node Element.

Please can someone help me with this..

Many Thanks & Regards

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
You'll need to keep track of the level of the last BAY_NO element you've parsed. Assuming that those elements can only be nested one level deep -not multiple levels- a boolean will suffice for this. You'd set it on the startElement method, and clear it in the endElement method.

As an aside, it looks as if the characters method is buggy. It assumes that something like "<tag>abc 123 def</tag>" will cause exactly one call to the characters method with a parameter of "abc 123 def". But that's not the case - that string can be split up into multiple consecutive calls, the string parameters of which will need to be concatenated.
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Thanks a lot for your response.

I have tried to modified the startElement and endElement methods like Ulf described:

I am setting the isRowCreated flag(line 20) in the startElement method and unsetting it in the endElement method(line 55). Now, I cannot get the child nodes which have different name than the parent node in my output. The values inside the BAY_NO tags are visible on my output jsp.It seems that my code doesn't recognise the other child nodes which have different name than the parent node name.However,This code works fine for the xml files where the parent node names and child node names are different.

Hope that I made sense with the above. Please advice

Thanks & Regards>
I agree. Here's the link:
subject: help needed with sax parser
It's not a secret anymore!