This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Handling same name tags using SAX parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Handling same name tags using SAX parser" Watch "Handling same name tags using SAX parser" New topic
Author

Handling same name tags using SAX parser

Satish Kandagadla
Greenhorn

Joined: Jul 03, 2006
Posts: 27
Hi All,

I have an XML with tags in the tree structure having similar name (Please see below). In the example below how can I differentiate the <Name tags> using a SAX parser?

<CategoryList>
<Category id="1900000021" >
<Name>Series</Name>
<CategoryList>
<Category>
<Name>ABC</Name>
<ForumList>
<Forum>
<Name>Test</Name>
</Forum>
</ForumList>
</Category>
</CategoryList>
</Category>
</CategoryList>

Is there a way to identify where does the Name tag belongs to in the tree structure?

Thanks,
Satish
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

How would you like to "differentiate" them? For example, do you want to know that they contain different text? Or do you want to know which comes first, second, and third? Whatever, you will have to start out by defining your requirements before you try to implement them.
Satish Kandagadla
Greenhorn

Joined: Jul 03, 2006
Posts: 27
Originally posted by Paul Clapham:
How would you like to "differentiate" them? For example, do you want to know that they contain different text? Or do you want to know which comes first, second, and third? Whatever, you will have to start out by defining your requirements before you try to implement them.


I want to capture the text of the second and third occurrences of the Name tag. The sample that I pasted is just an extract. The original XML has tree structure which is huge.

I was planning to count the occurrences and then take out the text out of it. But I also wanted to check if there is any other way of doing it.

Thanks,
Satish
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42357
    
  64
XPath makes it relatively easy to access the 2nd and 3rd occurrence directly, but it relies on the document structure not changing. SAX is more stable in the face of changing document structure.


Ping & DNS - my free Android networking tools app
Satish Kandagadla
Greenhorn

Joined: Jul 03, 2006
Posts: 27
Originally posted by Ulf Dittmer:
XPath makes it relatively easy to access the 2nd and 3rd occurrence directly, but it relies on the document structure not changing. SAX is more stable in the face of changing document structure.


Ok. But the possibility of using XPATH in my case is small as I would like to capture the names in Java Collections.

I think if I use Xquery I may probably do both (looking for occurrence and capturing it in collection). But I have already written a bunch of code in the parser and would like to add to it.

Thanks,
Satish
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

Originally posted by Satish Kandagadla:
I would like to capture the names in Java Collections.
Okay. So every time you start a Name element, create a StringBuffer. Then add characters to that in the characters() method. When you end a Name element, add the contents of that StringBuffer to a collection. It's just basic Java programming and any tutorial which has an example of SAX processing should depict something like that. ERH's book is a bit dated but it has chapters about SAX.
[ December 10, 2008: Message edited by: Paul Clapham ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling same name tags using SAX parser