I had a method to write XML files with FileWriter but discovered when I wanted to (…allow my customers to…) use special characters it fell over. I needed to use a FileOutputStream in order to deal with bytes.

Original method:

 
public void oldWriteXML() throws Exception{
        storeDoc.normalize(); //storeDoc is type Document
        if (storeFile!=null){
            storeFile.delete(); //delete the old file to overwrite
        }
        OutputStreamWriter fos = new FileWriter(filename);

        OutputFormat format = new OutputFormat(storeDoc);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        XMLSerializer serializer = new XMLSerializer(fos, format);
        serializer.serialize(storeDoc);

        fos.close();
    }

New method to allow UTF-8:

 
public void writeXML() throws Exception{
        storeDoc.normalize();
        if (storeFile!=null){
            storeFile.delete();
        }
        OutputStreamWriter fos = 
                new OutputStreamWriter(new FileOutputStream(filename),"UTF-8");
        
        OutputFormat format = new OutputFormat(storeDoc, "UTF-8", true);
        format.setLineWidth(65);
        format.setIndent(2);
        
        XMLSerializer serializer = new XMLSerializer(fos, format);
        serializer.serialize(storeDoc);

        fos.close();        
    }

Kim Debling is a Hampshire, UK based designer and Director of her own company Kestrel Design Ltd. She is mum to Rose and Harvey and wife to her best friend Steve. ¬†She’s fighting off Stage 4 Lymphoma and sharing her story along the way, mainly via YouTube. Kim is passionate about being happy, mental wellbeing and in particular art and creative pursuits as therapy during tough times. She teaches online at Udemy, has published books and has art and printables available for sale.

Writing UTF-8 XML files with Java was last modified: April 30th, 2015 by Kim Debling