0

I'm using a SaxHandler to parse log file from third-party app. My code:

public class SaxHandler extends DefaultHandler {

private String tempVal;
private String response;
private boolean readResponse;
boolean elementOn;

public SaxHandler() {
    readResponse = false;
    elementOn = false;
}

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

    if (qName.equalsIgnoreCase("method") || qName.equalsIgnoreCase("message")) {
        elementOn = true;
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    if (qName.equalsIgnoreCase("message")) {
        if (readResponse) {
            response        = tempVal;
            readResponse    = false;
        }
    }
    if (qName.equalsIgnoreCase("method")) {
        if (tempVal.equalsIgnoreCase("postHtml")) {
            readResponse = true;
        }
    }
}

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

    if (elementOn) {
        tempVal = new String(ch, start, length);
        elementOn = false;
    }

}

public String getLastResponse() {
    return response;
}
} 

Sample record in log file:

<record>
<date>07.07.2014 16:12:45</date>
<millis>1404735165155</millis>
<sequence>12</sequence>
<logger>logName</logger>
<level>FINER</level>
<class>ru.tsi.maksbm.android.revise.ReviseStartActivity</class>
<method>onStart</method>
<thread>0</thread>
<message>Starting ReviseStartActivity</message>
</record>

Parser is starting to parse normally but i'm getting SaxParser not well-formed error. I validate log file and it's failed on following line:

 <message>action=%D0%92%D1%85%D0%BE%D0%B4&j_username=U_USER&password=hash&</message>

with next error:

"The reference to entity "j_username" must end with the ';' delimiter"

Why parser didn't recognize this line as single string?

Max Plakhuta
  • 310
  • 2
  • 14

0 Answers0