Sabbir Ahmed Reyjohn
Sabbir Ahmed Reyjohn

Reputation: 23

How to ignore similar tag in XML SAX PARSING

I have XML like this one

<OuterTag>
       <Name>JAVA
      </Name>
<InnerTag>
     <Name> PHP
    </Name>
</InnerTag>

</OuterTag>

I just want That value which contains "Java". But when I parse it also brings "PHP" because the local names are the same. Is it possible to filter multiple LocalNames and select my desired one? How can I do that?

Upvotes: 0

Views: 920

Answers (3)

FlipMcF
FlipMcF

Reputation: 12877

Sax parsers typically have hooks where you can write code, specifically StartElement, EndElement and characters.

Moss has the right answer -

StartElement: Push the element name onto a stack.

characters: If the element name is 'name', and the stack has a "OuterTag" element on it, then you found your value. Otherwise, ignore it.

EndElement Pop the element off the stack.


Note that sax parsers are very powerful but sometimes overkill. Very fast, good for parsing malformed xml, or very large XML files, reacting to elements as the parser encounters them.

I would carefully suggest 'considering' an XPath Solution, that does the parsing work for you, allowing you to easily reference any element. Create an Xpath Object and query it with something like '/OuterTag/Name[1]' If you've used jQuery before, you'll be right at home.

However, if your XML is malformed or really large and complicated, this can be very slow. You've been warned.

Just know that XPath is available as a possible solution. http://www.javabeat.net/tips/182-how-to-query-xml-using-xpath.html

Upvotes: 0

MarcoS
MarcoS

Reputation: 13574

If I understnd correclty you want the Name tag under OuterTag and not those under InnerTag. So, this is how I would do it with dom4j:

    SAXReader saxReader = new SAXReader();
    saxReader.addHandler("OuterTag/Name", new ElementHandler() {

        @Override
        public void onStart(ElementPath arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onEnd(ElementPath arg0) {
            // TODO Auto-generated method stub

        }
    });

    File inputFile = new File(filename);
    saxReader.read(inputFile);

I hope this helps.

Upvotes: 0

Moss
Moss

Reputation: 6012

The idea is to save the state in which you are, just use a boolean value and set that to true if you find a open tag for 'OuterTag' and set it to false when you find a open tag for 'InnerTag'.

This way when you find the 'name' tag you now where you are in.

Another more flexible way is to push/pop the tag names when you find them. This way you can check who is your parent tag when you find a 'name' tag and then get the right value.

Upvotes: 1

Related Questions