简体   繁体   中英

Using SAX event handlers in Java, How do I keep variables across different events?

Basically I have an XMLfile that looks kinda like this:

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

This is the code for my SAX reader:

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

My issue. In the method characters, during the (fieldName) if block, I the value for tableNameString is still there. During the (table) if block, the value for tableNameString is still there, but the String I made in the (fieldName) block looses its value. I have printed out the String I build in the (fieldName) block and it builds what I need it to, but as soon as I'm in the (table) block it disappears, but the value for tableNameString doesn't. Why?

Edit: Well, I've realised why. The characters method gets called for the space between the <> and the . So of course the field list isn't built yet. So would putting it in the endElement method work? If not, why?

Try to change this part

    @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 + ", ");
        }
    }

BTW equalsIgnoreCase in XML processing is a bad idea, XML names are case-sensitive thus "tablename" and "TABLENAME" are 2 different tags. It's better to use

qName.equals("TABLENAME")) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM