I am using SAXParser for parsing the xml data received from server. I have created the abstract class XmlParser extends DefaultHandler with abstract methods characters(),startelement(),endelement().I have encountered this issue(WARN/ExpatReader(718): DTD handlers aren't supported.)My PArsing stops in the middle.I have referred some answers suggested to follow the below code My code
public abstract class XmlParser exten开发者_Python百科ds DefaultHandler {
public XmlParser(final String xmlData) {
parseDocument(xmlData);
}
@Override
public abstract void characters(char[] ch, int start, int length)
throws SAXException;
@Override
public abstract void endElement(String uri, String localName, String qName)
throws SAXException;
private void parseDocument(final String xmlData) {
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
SAXParser sp = spf.newSAXParser();
InputStream is = new ByteArrayInputStream(xmlData.getBytes("UTF-8"));<----I think i have to change here
sp.parse(is, this);
} catch (SAXException ex) {
ex.printStackTrace();
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public abstract void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException;
}
Referred answer
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); xmlReader.setContentHandler(//YourHandler extends DefaultHandler); xmlReader.parse(new InputSource(//BufferedReader));
problem for me i have an abstract class that extends defaulthandler. I cant create a object for it to pass in setContentHandler() method. I should keep this XmlParser as abstract since i have to override for each module parser So suggest me a solution to overcome this issue
This is a known bug. It was reported as fixed in FroYo (2.2).
精彩评论