2012-02-24 12 views
6

Sto analizzando XML utilizzando DocumentBuilder in java 1.4.
XML ha prima linea comeOttenere il tipo di codifica di un XML in java

xml version="1.0" encoding="GBK" 

voglio ottenere tipo di codifica del codice XML e utilizzarlo. Come posso ottenere "GBK"
Fondamentalmente creerò un altro XML in cui voglio che venga mantenuto il valore encoding="GBK".
Attualmente si sta perdendo e impostato sul valore predefinito UTF-8
Ci sono molti XML con codifica diversa che ho bisogno di leggere la codifica della sorgente e quindi le cose necessarie.

Si prega di aiutare

+2

'org.w3c.dom.Document.getXmlEncoding()' ?? – artbristol

+0

Anche se questo è vecchio: c'è una dichiarazione W3C ufficiale: https://www.w3.org/TR/xml/#sec-guessing –

risposta

0

Utilizzando javax.xml.stream.XMLStreamReader per analizzare il file, quindi è possibile chiamare getEncoding().

+0

bt ne ho bisogno in java 1.4 – user1228785

4

Un modo per questo funziona in questo modo

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(); 
+0

Sul mio computer MS Windows, 'getEncoding () '* always * restituisce' null'. 'getCharacterEncodingScheme()' restituisce solo la codifica dichiarata che il file * non * ha un marchio di ordine byte UTF-8, altrimenti anche 'null'. – Paramaeleon

1

Questo funziona per varie codifiche, tenendo riguardano sia la distinta base e la dichiarazione XML. Il valore predefinito è UTF-8 se non si applica:

String encoding; 
FileReader reader = null; 
XMLStreamReader xmlStreamReader = null; 
try { 
    InputSource is = new InputSource(file.toURI().toASCIIString()); 
    XMLInputSource xis = new XMLInputSource(is.getPublicId(), is.getSystemId(), null); 
    xis.setByteStream(is.getByteStream()); 
    PropertyManager pm = new PropertyManager(PropertyManager.CONTEXT_READER); 
    for (Field field : PropertyManager.class.getDeclaredFields()) { 
     if (field.getName().equals("supportedProps")) { 
      field.setAccessible(true); 
      ((HashMap<String, Object>) field.get(pm)).put(
        Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY, 
        new XMLErrorReporter()); 
      break; 
     } 
    } 
    encoding = new XMLEntityManager(pm).setupCurrentEntity("[xml]".intern(), xis, false, true); 
    if (encoding != "UTF-8") { 
     return encoding; 
    } 

    // From @matthias-heinrich’s answer: 
    reader = new FileReader(file); 
    xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(reader); 
    encoding = xmlStreamReader.getCharacterEncodingScheme(); 

    if (encoding == null) { 
     encoding = "UTF-8"; 
    } 
} catch (RuntimeException e) { 
    throw e; 
} catch (Exception e) { 
    throw new UndeclaredThrowableException(e); 
} finally { 
    if (xmlStreamReader != null) { 
     try { 
      xmlStreamReader.close(); 
     } catch (XMLStreamException e) { 
     } 
    } 
    if (reader != null) { 
     try { 
      reader.close(); 
     } catch (IOException e) { 
     } 
    } 
} 
return encoding; 

provata su Java 6 con:

  • UTF-8 file XML con distinta base, con la dichiarazione XML ✓
  • UTF-8 file XML senza BOM, con dichiarazione XML ✓
  • UTF-8 File XML con BOM, senza dichiarazione XML ✓
  • UTF-8 File XML senza BOM, senza XML Dichiarazione ✓
  • ISO-8859-1 file XML (senza BOM), con dichiarazione XML ✓
  • UTF-16LE file XML con BOM, senza dichiarazione XML ✓
  • UTF-16BE file XML con BOM, senza dichiarazione XML ✓

Standing sulle spalle di questi giganti:

import java.io.*; 
import java.lang.reflect.*; 
import java.util.*; 
import javax.xml.stream.*; 
import org.xml.sax.*; 
import com.sun.org.apache.xerces.internal.impl.*; 
import com.sun.org.apache.xerces.internal.xni.parser.*;