• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java code and SAX

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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

<studentTree>
<student id="1">
<name>jason</name>
<subject>Maths</subject>
<subject>Chemistry></subject>
<student>
<student id="2">
<name>John</name>
<subject>Science</subject>
<subject>History</subject>
<student>
</studentTree>

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
Value:2
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 ?

 
Saloon Keeper
Posts: 7590
177
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 734
7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic