aspose file tools
The moose likes XML and Related Technologies and the fly likes java code and SAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "java code and SAX" Watch "java code and SAX" New topic

java code and SAX

J amodi

Joined: Nov 09, 2011
Posts: 18
Hi all,

I am having one xml file which has contents something like this.

<student id="1">
<student id="2">

I am parsing this XML using SAX parser. I have written a code something like this.

I want to get values from my xml into objects.
for example,

1st iteration
studentinfo.getId() should give me value :1
studentInfo.getSubjects() should give me : Maths,Chemistry

2nd iteration
studentInfo.getSubjects() should give me : science,history.

The problem is, I am not able to get subject values in the list as the element name(subject) is same. Could anyone please help me out in this ?

Tim Moores

Joined: Sep 21, 2011
Posts: 2409
You shouldn't create a new StringBuffer each time a subject tag is encountered. You should create it exactly once, and then clear it each time a student tag is closed.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 569
@Jasoncool Smith

[0] You have major typos in the xml. And then, you have not cleaned up the code StudentInfo or PageInfo class, you've to pick one and use consistently.

[1] Then apart from those, the logic of the sax parsing is clearly defective. Here is a major rewrite based on it (I pick PageInfo).
J amodi

Joined: Nov 09, 2011
Posts: 18
Hi g tsuji,

Thanks a lot for reply. Yea, I had some typos in the code. Sorry about that. Your answer resolved my problem.

Hi Tim Moores,

Thanks for your reply too.

I agree. Here's the link:
subject: java code and SAX