aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "help needed with sax parser" Watch "help needed with sax parser" New topic
Author

help needed with sax parser

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Hi,

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>
<BAY_NO><![CDATA[BAY 392500]]></BAY_NO>
</BAY_NO>
<BAY_NO>
<BAY_NO><![CDATA[BAY 408506]]></BAY_NO>
<MOL_WT><![CDATA[300.1913]]></MOL_WT>
<MOL_FORMULA><![CDATA[C12 H7 F3 N2 O4]]></MOL_FORMULA>
<EXACT_MASS><![CDATA[300.03579]]></EXACT_MASS>
</BAY_NO>
<BAY_NO>
<BAY_NO><![CDATA[BAY 408761]]></BAY_NO>
<MOL_WT><![CDATA[388.3799]]></MOL_WT>
<MOL_FORMULA><![CDATA[C19 H29 N3 O . 2 Cl H]]></MOL_FORMULA>
<EXACT_MASS><![CDATA[316.23889]]></EXACT_MASS>
</BAY_NO>
<BAY_NO>
<BAY_NO><![CDATA[BAY 579602]]></BAY_NO>
<MOL_FORMULA><![CDATA[*STRUCTURE]]></MOL_FORMULA>
</BAY_NO>
<BAY_NO>
<BAY_NO><![CDATA[BAYB005342]]></BAY_NO>
<MOL_WT><![CDATA[380.8481]]></MOL_WT>
<MOL_FORMULA><![CDATA[((C19 H29 N3 O)c(Zn)c)mix]]></MOL_FORMULA>
</BAY_NO>

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

<TEST_DC_2005_PURCHASING>
<UNLOAD_DATE>2007-10-02</UNLOAD_DATE>
<PO_NUM>A4568KCM1</PO_NUM>
<PO_RELEASE/>
<PO_DATE>2000-08-31</PO_DATE>
<PO_BUYER>KC</PO_BUYER>
</TEST_DC_2005_PURCHASING>

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
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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.


Ping & DNS - my free Android networking tools app
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>
 
Don't get me started about those stupid light bulbs.
 
subject: help needed with sax parser