views:

28

answers:

1

I have to parse a XML structure in JAVA using the SAX parser. The problem is that the structure is recursive with an unspecified count of recursions. This still is not such a big deal the big deal is that I can't take advantage of the XML namespace functionality and the tags are the same on every recursion level.

Here is an example of the structure.

<?xml version="1.0" encoding="UTF-8"?>
<RootTag>
    <!-- LOADS OF OTHER TAGS -->
    <Tags attribute="value">
        <Tag attribute="value">
            <SomeOtherTag></SomeOtherTag>
            <Tags attribute="value">
                <Tag attribute="value">
                    <SomeOtherTag></SomeOtherTag>
                    <Tags attribute="value">
                        <!-- MORE OF THE SAME STRUCTURE -->
                    </Tags>
                </Tag>
            </Tags>
        </Tag>
    </Tags>
    <!-- LOADS OF OTHER TAGS -->
</RootTag>

As you can see there is a recursion better an undefined number of recursions. Now my problem is how to extract all data for every recursion and save it in a HashMap for example.

I could define a ContentHandler for the occurrence of Tags and have him extract the content in a HashMap and put it back in a "master" HashMap defined in the main content handler. But I'm not sure hot to do this.

How do I extract and save the content of a recursive XML structure without using namespaces.

I appreciate your help.

+2  A: 

Check out this set of Javaworld articles on using SAX. It demonstrates an easy way to parse a recursive XML structure using SAX.

Nathan Hughes
Thanks a lot. This is quite exactly what I was looking for.
Octavian Damiean