Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …
Suggested Courses

618 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