Search code examples
javaxmlunicodeencodingstax

How to use StAX parser with umlauts (ÄÜÖ)?


I'm using the StreamReaderDelegate and want to use umlauts (ÄÜÖ).

My initialisation of the StAX parser:

public class GraphDataStreamReader extends StreamReaderDelegate {
  public GraphDataStreamReader(XMLStreamReader streamReader) throws XMLStreamException {
    super(streamReader);
  }
  ...
}

....

XMLInputFactory factory = XMLInputFactory.newInstance();
graphSR = new GraphDataStreamReader(
            factory.createXMLStreamReader(new FileReader(xmlFile)));
  • In the XMLFile: Grenzübergang Mesenich LU->DE
  • Out getAttributeValue(x): Grenzübergang Mesenich LU->DE

My XML File is marked with UTF-8 (also tried 16, 32).

What can i do?


Solution

  • The FileReader uses the platform's default character encoding and will corrupt the file content if your XML file uses a different encoding.

    Use a FileInputStream instead:

    factory.createXMLStreamReader(new FileInputStream(xmlFile))