Details
-
Bug
-
Status: Resolved
-
Major
-
Resolution: Incomplete
-
2.6.2
-
None
-
None
Description
Hi,
I'm having problems determining the encoding declared
in the XML document using SAX. I've followed the
instructions I've found in the FAQ to retrieve the
encoding, but I always get 'null' as encoding. The
code below illustrates my problem:
import java.io.StringReader;
import java.lang.reflect.Method;
import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.helpers.DefaultHandler;
public class XercesTest {
public static void main(String[] args) throws
Exception {
String xml = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?>\n<root/>";
SAXParser parser = new SAXParser();
InputSource source = new InputSource(new
StringReader(xml));
parser.setContentHandler(new DefaultHandler()
public void startDocument()
{ System.out.println("encoding: " + getEncoding(locator)); }public void endDocument()
{ System.out.println("encoding: " + getEncoding(locator)); } private String getEncoding(Locator loc) {
String encoding = null;
Method getEncoding = null;
try {
getEncoding =
loc.getClass().getMethod("getEncoding", new
Class[]{});
if(getEncoding != null)
} catch (Exception e) {
}
return encoding;
}
});
parser.parse(source);
}
}
The output of the code above is:
encoding: null
encoding: null
Maarten