Solved

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

Posted on 2010-09-06
4
1,313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 500 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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

730 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