How to skip the parent tag, based on the value of a child tag in XML Parsing using SAX Parser

Vipul Reddy
<root>
<parent>
    <child1> 30</child1>
    <child2> 30</child2>
    <child3> 30</child3>
</parent>
 <parent>
    <child1> 20</child1>
    <child2> 30</child2>
    <child3> 30</child3>
</parent>
 <parent>
    <child1> 30</child1>
    <child2> 30</child2>
    <child3> 30</child3>
</parent>
</root>

I am really new to the world of coding and to sax parsing.. Consider the above XML, what I need is. .. based on the value of the tag child1, if it is greater than 20, only then I would want to parse the remaining child tags(child2 and child3), otherwise I would want to move on to the next parent tag.

Could anyone pls suggest what would be the ideal way to do it?

Maurice Perry

Something like that:

...
private boolean skipChildren;
private StringBuilder buf = new StringBuilder();
...

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    if (qName.equals("parent")) {
        skipChildren = false;
        ...
    } else if (qName.equals("child1")) {
        buf.setLength(0);
        ...
    } else if (qName.startsWith("child")) {
        if (!skipChildren) {
            buf.setLength(0);
            ...
        }
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (qName.equals("parent")) {
        ...
    } else if (qName.equals("child1")) {
        int value = Integer.parseInt(buf.toString().trim());
        if (value <= 20) {
            skipChildren = true;
        }
        ...
    } else if (qName.startsWith("child")) {
        if (!skipChildren) {
            int value = Integer.parseInt(buf.toString().trim());
            doSomethingWith(value);
        }
    }
}

@Override
public void characters(char[] ch, int start, int length) {
    if (!skipChildren) {
        buf.append(ch, start, length);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unable to Parse Parent / Child Elements using SAX XML Parser (Android)

From Dev

Getting Parent Child Hierarchy in Sax XML parser

From Dev

Parsing XML using SAX Parser in Python 3

From Dev

Parsing and updating xml using SAX parser in java

From Dev

Parsing XML using SAX Parser in Python 3

From Dev

How to remove XML tag based on child attribute using php?

From Dev

How to retrieve value of XML element using a Nokogiri SAX Parser?

From Dev

VTD XML parsing. Get value based on tag and attribute

From Dev

Read parent and child at the same time using SAX Parser in Java

From Dev

get individual child value from parent tag using onclick method

From Dev

Add Attribute based on its Parent tag using linq to xml

From Dev

Sax parser takes wrong tag Android

From Dev

How to get value of child tag of a button tag

From Dev

How to remove xml parent tag using XML DOM javascript?

From Dev

xslt how to remove the parent tag and change attributes of a child tag in an xml doc

From Dev

Android XML using SAX Parser

From Dev

Parsing an XML file with Nokogiri SAX parser

From Dev

parsing xml with python, selecting a tag using a sibling tag as selector

From Dev

parsing xml with python, selecting a tag using a sibling tag as selector

From Dev

How to Parse XML without using DOM or SAX Parser in Java?

From Dev

parsing XML in Java using SAX: value cut in 2 halves

From Dev

parsing XML in Java using SAX: value cut in 2 halves

From Dev

How to wrap child elements in a parent tag?

From Dev

How to get previous tag (td) child attribute value using jquery?

From Dev

How I need to get tag name of sub child in xml by comparing the attribute value and given string value using Java?

From Dev

Parsing Hyphenated xml Tag

From Dev

Issue with xml parsing to get child tag values in Java

From Dev

how to find the node which has child node while parsing XML file using DOM parser in java

From Dev

how to find the node which has child node while parsing XML file using DOM parser in java

Related Related

  1. 1

    Unable to Parse Parent / Child Elements using SAX XML Parser (Android)

  2. 2

    Getting Parent Child Hierarchy in Sax XML parser

  3. 3

    Parsing XML using SAX Parser in Python 3

  4. 4

    Parsing and updating xml using SAX parser in java

  5. 5

    Parsing XML using SAX Parser in Python 3

  6. 6

    How to remove XML tag based on child attribute using php?

  7. 7

    How to retrieve value of XML element using a Nokogiri SAX Parser?

  8. 8

    VTD XML parsing. Get value based on tag and attribute

  9. 9

    Read parent and child at the same time using SAX Parser in Java

  10. 10

    get individual child value from parent tag using onclick method

  11. 11

    Add Attribute based on its Parent tag using linq to xml

  12. 12

    Sax parser takes wrong tag Android

  13. 13

    How to get value of child tag of a button tag

  14. 14

    How to remove xml parent tag using XML DOM javascript?

  15. 15

    xslt how to remove the parent tag and change attributes of a child tag in an xml doc

  16. 16

    Android XML using SAX Parser

  17. 17

    Parsing an XML file with Nokogiri SAX parser

  18. 18

    parsing xml with python, selecting a tag using a sibling tag as selector

  19. 19

    parsing xml with python, selecting a tag using a sibling tag as selector

  20. 20

    How to Parse XML without using DOM or SAX Parser in Java?

  21. 21

    parsing XML in Java using SAX: value cut in 2 halves

  22. 22

    parsing XML in Java using SAX: value cut in 2 halves

  23. 23

    How to wrap child elements in a parent tag?

  24. 24

    How to get previous tag (td) child attribute value using jquery?

  25. 25

    How I need to get tag name of sub child in xml by comparing the attribute value and given string value using Java?

  26. 26

    Parsing Hyphenated xml Tag

  27. 27

    Issue with xml parsing to get child tag values in Java

  28. 28

    how to find the node which has child node while parsing XML file using DOM parser in java

  29. 29

    how to find the node which has child node while parsing XML file using DOM parser in java

HotTag

Archive