在Java中使用SAX事件处理程序,如何在不同事件中保留变量?

时间:2013-05-04 03:05:11

标签: java xml-parsing sax

基本上我有一个看起来像这样的XML文件:

 <?xml version="1.0" encoding="UTF-8"?>
 <DATABASE>
 <TABLE>
    <TABLENAME>emp</TABLENAME>
    <FIELDNAME type="boolean">a</FIELDNAME>
    <FIELDNAME type="char(3)">b</FIELDNAME>
    <FIELDNAME type="date">c</FIELDNAME>
    <FIELDNAME type="integer">d</FIELDNAME>
    <FIELDNAME type="real">e</FIELDNAME>
    <FIELDNAME type="varchar">f</FIELDNAME>

</TABLE>

这是我的SAX阅读器的代码:

public class SAXReader {
public SAXReader() throws SAXException, IOException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(new ContentHandler());
    reader.parse("database.xml");
}

private class ContentHandler extends DefaultHandler {
    private boolean table = false;
    private boolean tableName = false;
    private boolean fieldName = false;
    private String fieldType;
    private String tableNameString = "";
    private String fieldList = "";

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("tablename")) {
            tableName = true;

        }
        if (qName.equalsIgnoreCase("fieldname")) {
            fieldName = true;
            fieldType = attributes.getValue("type");
        }
        if (qName.equalsIgnoreCase("table")) {
            table = true;
        }
    }

    /*
     * This fires whenever the XMLReader encounters a </>
     */
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if (tableName) {
            tableName = false;
        }
        if (fieldName) {
            fieldName = false;
        }
        if (table) {                
            table = false;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        String data = new String(ch, start, length); 
        // test against stored values
        if (tableName) {
            tableNameString = data;
        }
        if (fieldName) {
            fieldList += (data+ " " + fieldType + ", ");
        }
        if (table){
            try {
                Database.database.addTable(tableNameString, fieldList);
            } catch (MyException e) {
                e.printStackTrace();
            }
        }
    }
}
}

我的问题。在方法字符中,在(fieldName)if块期间,我仍然存在tableNameString的值。在(table)if块期间,tableNameString的值仍然存在,但是我在(fieldName)块中创建的String将丢失其值。我打印出了我在(fieldName)块中构建的String,它构建了我需要它的东西,但是一旦我在(table)块中它就消失了,但tableNameString的值却没有。为什么呢?

编辑:嗯,我已经意识到了原因。为&lt;&gt;之间的空间调用characters方法和。当然,字段列表尚未构建。那么将它放在endElement方法中工作吗?如果没有,为什么?

1 个答案:

答案 0 :(得分:1)

尝试更改此部分

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("tablename")) {
            tableName = false;
        } else if (qName.equalsIgnoreCase("fieldname")) {
            fieldName = false;
        } else if (qName.equalsIgnoreCase("table")) {
            try {
                Database.database.addTable(tableNameString, fieldList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            fieldList = "";
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String data = new String(ch, start, length);
        if (tableName) {
            tableNameString = data;
        } else if (fieldName) {
            fieldList += (data + " " + fieldType + ", ");
        }
    }
XML处理中的BTW equalsIgnoreCase是个坏主意,XML名称区分大小写,因此“tablename”和“TABLENAME”是2个不同的标签。最好使用

qName.equals("TABLENAME"))