Handling Empty Tags in XML using Sax Parser, Java

I'm Using Sax parser to handle a pre written xml file....i have no way of changing the xml as it is held by another application but need to parse data from it. The Xml file contains a Tag < ERROR_TEXT/> which is empty when no error is occurred. as a result the parser takes the next character after the tag close which is "n". i have tried result.replaceAll("n", ""); and result.replaceAll("n", "");

how to i get sax to recognise this is an empty tag and return the value as "" ?


You don't. It is SAXs job parse the data, not to make decisions on what the content of that data is supposed to be. In your parseHandler, store the string of the data in all your element, and when you go to process that element, do a string.trim() on the data. if the output of that is blank and your tag is an ERROR_TEXT tag, you know there is no error.


SAXParser returns cDAta through the characters() event which it calls whenever it encounters 'characters' literally. It's pointless to use that function as it is called after every open tag regardless of whether it actually contains any data. You could use String.trim() and do a String.length()>=0 check before proceeding.


You DO THAT. If you have xml and Java source blow.

<ERROR_TEXT>easy</ERROR_TEXT><ERROR_TEXT/>

Java code

private boolean isKeySet = false;
private String key = "";
@Override
public void characters(
    char[] ch,
    int start,
    int length
) throws SAXException
{
    if (!isKeySet) {
        return;
    }
    isKeySet = false;
    logger.debug("key : [" + key + "], value : [" + value + "]");
}
@Override
public void startElement(
    String uri,
    String localName,
    String qName,
    Attributes attrs
) throws SAXException
{
    key = qName;
    isKeySet = true;
}

@Override
public void endElement(
    String uri,
    String localName,
    String qName
) throws SAXException
{
    if (isKeySet) {
        isKeySet = false;
        logger.debug("key : [" + key + "](EMPTY!!!)");
    }
}

RESULT log:

key : [ERROR_TEXT], value : [easy]

key : [ERROR_TEXT](EMPTY!!!)

Call flow: startElement() -> characters() -> endElement() -> startElement() -> endElement() -> characters()

That's it! THE END

链接地址: http://www.djcxy.com/p/34914.html

上一篇: 让SAXParser忽略转义字符

下一篇: 使用Sax Parser,Java处理XML中的空标签