I am parsing XML using DocumentBuilder in java 1.4.
XML has first line as
xml version="1.0" encoding="GBK"
I want to get encoding type of the XML and use it. How can I get "GBK"
Basically i will be making one more XML where i want encoding="GBK" to be retained.
Currently it is getting lost and set to default UTF-8
There are many XML with different encoding I need to read encoding od source and so necessary things.
Please help
解决方案
One way to this works like this
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( new FileReader( testFile ) );
//running on MS Windows fileEncoding is "CP1251"
String fileEncoding = xmlStreamReader.getEncoding();
//the XML declares UTF-8 so encodingFromXMLDeclaration is "UTF-8"
String encodingFromXMLDeclaration = xmlStreamReader.getCharacterEncodingScheme();