<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?
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.
Comments