Solved

Does XML Bean support character encodings other than UTF-8?

Posted on 2010-09-06
4
1,298 Views
Last Modified: 2013-11-24
Hello,

As part of my project, we have developed web services using AXIS(for soap web service) and jersey(rest web service) in JAVA. We are using XML Beans framework for generating the XML response.
Here is the issue I am facing:
While generating the response document type object from XML Bean object, I am getting IOException.
Please find below the java code which generates the document type object from XML Bean object.

    public Document generateXMLDocument(XmlObject object) throws AxisFault {
        InputStream is = null;        
        Document doc = null;
        XmlOptions options = new XmlOptions();
        options.setSavePrettyPrint();
        options.setCharacterEncoding("ISO-8859-1");
        // Tab size for a child element
        options.setSavePrettyPrintIndent(4);

        String xmlStr = null;
        xmlStr = object.xmlText(options) ;

        LogHelper.getLogger().debug(xmlStr);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        try {
            is = new ByteArrayInputStream(xmlStr.getBytes("ISO-8859-1"));
            doc = dbf.newDocumentBuilder().parse(is);
        } catch (UnsupportedEncodingException e) {
             throw new AxisFault("INVALID.ENCODING",
                "The XML is in unrecognized Encoding Format", null, null);
        } catch (SAXException e) {
             throw new AxisFault("PARSING.ERROR",
                "Error while parsing XML", null, null);
        } catch (IOException e) {
            e.printStackTrace();
             throw new AxisFault("INTERNAL.ERROR",
                "Internal Server Error", null, null);
        } catch (ParserConfigurationException e) {
             throw new AxisFault("PARSING.ERROR",
                "Error while parsing XML", null, null);
        }    
        return doc;
    }
}

1) Since my database has ISO-8859-1 charset, I needed to set the character encoding in the above method as ISO-8859-1 only.
2) Also while doing the input stream, I used xmlStr.getBytes("ISO-8859-1") as mentioned in the above method.

The exception is thrown in the 2nd point. When I use xmlStr.getBytes("UTF-8"), its working fine. Does it mean XML Bean does not support character encodings other than UTF-8.

Since my data is getting captured in ISO 8859-1 format in the database, I would like to set the xml response as well in ISO 8859-1.

Please let me know what I am missing here?

Regards
Siva
0
Comment
Question by:sivi_3883
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 total points
Comment Utility
try this,

new String(xmlStr.getBytes("UTF-8"), "ISO-8859-1").getBytes() ;
0
 

Author Comment

by:sivi_3883
Comment Utility
Thanks for your reply ksivananth.

I replaced the code from a) to b)
a) is = new ByteArrayInputStream(xmlStr.getBytes("ISO-8859-1"));
                 TO
b) is = new ByteArrayInputStream(new String(xmlStr.getBytes("UTF-8"), "ISO-8859-1").getBytes());

Still, it is throwing exception.

Actually, I forgot to add this point in my question:
Whenever there is a french or espanol character(anything other than EN characters) in xmlstr, then only exception is thrown. Its working fine for EN characters for both cases a) and b)

Thanks,
Siva
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 500 total points
Comment Utility
looks like ISO-8859-1 can not handle the french characters... you need to use appropriate charset.

right time to set your DB to store in UTF, then you don't have to worry about conversion...
0
 

Author Comment

by:sivi_3883
Comment Utility
Yes. Converting the Database charset to UTF should avoid these conversions.
Definitely we will do this conversion as soon as possible.

But currently the database is iso-8859-1and it stores french and espanol characters. I know finding a fix for this issue is too late but I need to push this code to production in 2 days:(
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now