?
Solved

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

Posted on 2010-09-06
4
Medium Priority
?
1,324 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 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 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