?
Solved

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

Posted on 2010-09-06
4
Medium Priority
?
1,347 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 2000 total points
ID: 33615264
try this,

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

Author Comment

by:sivi_3883
ID: 33615340
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 2000 total points
ID: 33615348
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
ID: 33615454
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

862 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