I have the method
public String parseRootElement(InputStream xmlStream) throws SAXException {
String name;
try {
DefaultHandler handler;
handler = new DefaultHandler() {
@Override
public void startElement(String uri,String localName,String qName,Attributes attributes){
name=qName;
}
};
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
parser.parse(xmlStream, handler);
return name;
} catch (ParserConfigurationException | IOException ex) {
throw new IllegalStateException("ParserConfigurationException", ex);
}
}
How can I get qName from public void startElement() ? name=qName doesnt work, because of
local variable input is accessed within inner class; needs to be declared final
There are a lot of similar questions but I still be dumb.
Change String name;
to final String[] name = new String[1];
, name=qName;
to name[0]=qName;
and return name;
to return name[0];
.
That creates an array where the result is stored in and retrieved from. By storing the result in an array lets us make the "name" variable final and thus the compiler happy.
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.