XML Parsing using SAX Parser 如何根据子标签的值跳过父标签

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

<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>

我对编码和 sax 解析的世界真的很陌生.. 考虑上面的XML,我需要的是。 .. 基于标签child1的值,如果它大于20,那么我才想解析剩余的子标签(child2和child3),否则我想继续下一个父标签。

谁能建议最好的方法是什么?

类似的东西:

...
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);
    }
}

下面是使用 vtd-xml, it is the state of art in xml processing technology, and is a lot more efficient and simpler to write than SAX... the key is by using xpath expression to filter out only the nodes of interest... read this paper 执行任务的代码,它为您提供了尽可能避免 SAX 解析的大量理由

Processing XML with Java – A Performance Benchmark

import com.ximpleware.*;
public class conditionalSelection {
    public static void main(String s[]) throws VTDException{
        VTDGen vg = new VTDGen();
        if(!vg.parseFile("d:\xml\condition.xml", false)) // disable namespace
            return;
        VTDNav vn = vg.getNav();
        AutoPilot ap = new AutoPilot(vn);
        ap.selectXPath("/root/parent[child1>20]"); // the xpath selecting all parents with child1>20
        int i=0,j=0;
        while((i=ap.evalXPath())!=-1){
            // now move the cursor to child2 and child3
            if(vn.toElement(VTDNav.FC,"child2")){
                j = vn.getText();
                if (j!=-1)//make sure the text node exist
                    System.out.println(" child2's text node is ==>"+ vn.toString(j));
                vn.toElement(VTDNav.P);
            }
            if(vn.toElement(VTDNav.FC,"child3")){
                j = vn.getText();
                if (j!=-1)//make sure the text node exist
                    System.out.println(" child3's text node is ==>"+ vn.toString(j));
                vn.toElement(VTDNav.P);
            }
        }
    }