1

基本上我有一个看起来像这样的 XMLfile:

 <?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) 块中创建的字符串失去了它的值。我已经打印出我在 (fieldName) 块中构建的字符串,它构建了我需要的内容,但是一旦我进入 (table) 块,它就会消失,但 tableNameString 的值不会。为什么?

编辑:嗯,我已经意识到为什么了。对于 <> 和 . 所以当然还没有建立字段列表。那么将它放在 endElement 方法中会起作用吗?如果不是,为什么?

4

1 回答 1

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 处理中的 equalsIgnoreCase 是一个坏主意,XML 名称区分大小写,因此“tablename”和“TABLENAME”是 2 个不同的标签。最好用

qName.equals("TABLENAME")) 
于 2013-05-04T03:42:39.560 回答