sivi_3883
asked on
Does XML Bean support character encodings other than UTF-8?
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(XmlObj ect object) throws AxisFault {
InputStream is = null;
Document doc = null;
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint ();
options.setCharacterEncodi ng("ISO-88 59-1");
// Tab size for a child element
options.setSavePrettyPrint Indent(4);
String xmlStr = null;
xmlStr = object.xmlText(options) ;
LogHelper.getLogger().debu g(xmlStr);
DocumentBuilderFactory dbf = DocumentBuilderFactory.new Instance() ;
dbf.setNamespaceAware(true );
try {
is = new ByteArrayInputStream(xmlSt r.getBytes ("ISO-8859 -1"));
doc = dbf.newDocumentBuilder().p arse(is);
} catch (UnsupportedEncodingExcept ion e) {
throw new AxisFault("INVALID.ENCODIN G",
"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 (ParserConfigurationExcept ion 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
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(XmlObj
InputStream is = null;
Document doc = null;
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint
options.setCharacterEncodi
// Tab size for a child element
options.setSavePrettyPrint
String xmlStr = null;
xmlStr = object.xmlText(options) ;
LogHelper.getLogger().debu
DocumentBuilderFactory dbf = DocumentBuilderFactory.new
dbf.setNamespaceAware(true
try {
is = new ByteArrayInputStream(xmlSt
doc = dbf.newDocumentBuilder().p
} catch (UnsupportedEncodingExcept
throw new AxisFault("INVALID.ENCODIN
"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 (ParserConfigurationExcept
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-
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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:(
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:(
ASKER
I replaced the code from a) to b)
a) is = new ByteArrayInputStream(xmlSt
TO
b) is = new ByteArrayInputStream(new String(xmlStr.getBytes("UT
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