在使用SAX解析器时如何获取父节点?

时间:2011-12-27 13:05:25

标签: java xml sax

我需要在java中使用SAX解析器解析文档。如果我使用传统上实现startElement,endElement和characters方法的DefaultHandler类,我能够打印所有节点值。我们如何访问子节点上的前一个节点值,我该怎么做? 我的示例XML是:

<staff>
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff>
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>

根据工资节点值,我还想访问第一个名字。我很迷惑。我们怎么做?我的示例代码:

SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {

    boolean bfname = false;
    boolean blname = false;
    boolean bnname = false;
    boolean bsalary = false;

    public void startElement(String uri, String localName,String qName, 
                Attributes attributes) throws SAXException {

        System.out.println("Start Element :" + qName);

        if (qName.equalsIgnoreCase("FIRSTNAME")) {
            bfname = true;
        }

        if (qName.equalsIgnoreCase("LASTNAME")) {
            blname = true;
        }

        if (qName.equalsIgnoreCase("NICKNAME")) {
            bnname = true;
        }

        if (qName.equalsIgnoreCase("SALARY")) {
            bsalary = true;
        }

    }

    public void endElement(String uri, String localName,
        String qName) throws SAXException {

        System.out.println("End Element :" + qName);

    }

    public void characters(char ch[], int start, int length) throws SAXException {

        if (bfname) {
            System.out.println("First Name : " + new String(ch, start, length));
            bfname = false;
        }

        if (blname) {
            System.out.println("Last Name : " + new String(ch, start, length));
            blname = false;
        }

        if (bnname) {
            System.out.println("Nick Name : " + new String(ch, start, length));
            bnname = false;
        }

        if (bsalary) {
            //System.out.println("Salary : " + new String(ch, start, length));
                    String nodeValue=new String(ch, start, length);
                    if(nodeValue.compareTo("100000")==0)
                   {
                     **????I need to store the respective respective first name
                         in ArrayList**
                    }
            bsalary = false;
        }

    }

     };

2 个答案:

答案 0 :(得分:3)

使用SAX时无法来回导航。你应该尝试使用DOM。如果您必须使用SAX,那么您可以使用Stack保存以前的数据并根据需要弹出它们。

答案 1 :(得分:1)

您可以使用String变量将名称存储为

public void characters(char ch[], int start, int length) throws SAXException {
    ... Code Here ...       
    if (bfname) {
        employeeName = new String(ch, start, length);
        bfname = false;
    }
    ... Code Here ...
}

&安培;

最后使用此变量
    public void characters(char ch[], int start, int length) throws SAXException {
    ... Code Here ...
    if (bsalary) {
        String nodeValue=new String(ch, start, length);\
            if(nodeValue.compareTo("100000")==0)
            {
                //Use employeeName Here...
            }
        bsalary = false;
    }
    ... Code Here ...
}