vikas31
asked on
facing problem in reading & writing the files
Hi,
I have to read & write the files in the Hungarian Language. Can anybody tell me which Cahracter set I should use for read & write of the file?getting caused by : sun.io.MalformedInputExcep tion.
Vikas
I have to read & write the files in the Hungarian Language. Can anybody tell me which Cahracter set I should use for read & write of the file?getting caused by : sun.io.MalformedInputExcep
Vikas
ASKER
I am using the same but facing problem when applying xslt to the xml the resulting xml have default encoding set to UTF-8 how I can change it during transformation?
Not sure. Can you post some code around the area you're doing that?
ASKER
public String map(MapperParams params) throws MapperException, CBSException {
logger.debug("Executing map");
logger.debug("mapping parameters" + params.toString());
String fileExtension = null;
this.xslMappingFile = params.getXslMappingFile() ;
this.inputFile = params.getInputFile();
logger.debug("this.inputFi le....."+i nputFile);
String fileType = "";
switch (params.getFileType()) {
case MapperParams.WIT : {
fileType="_WIT";
fileExtension = ".xml";
break;
}
case MapperParams.MIB : {
fileType="_MIB";
fileExtension = ".txt";
break;
}
default :
break;
}
this.witXmlFile = inputFile + fileType + fileExtension;
logger.debug("the new output "+fileType+" XMLFile " + this.witXmlFile);
DocumentBuilderFactory dFactory = DocumentBuilderFactory.new Instance() ;
dFactory.setNamespaceAware (true);
// creating DocumentBuilder
DocumentBuilder dBuilder = null;
try {
dBuilder = dFactory.newDocumentBuilde r();
}
catch (ParserConfigurationExcept ion e) {
String msg = "DocumentBuilder cannot be created satisfying the configuration requested";
throw new MapperException(MapperErro rCode.MAPP ER_MAP_INI T, this.getClass(), e, msg, "domfactory.newDocumentBui lder() | DocumentBuilder cannot be created");
}
// creating XSL Document for mapping
Document xslDoc = null;
try {
xslDoc = dBuilder.parse(FileLoading Utils.getR esourceAsI nputStream (xslMappin gFile));
}
catch (SAXException e) {
String msg = "Error parsing the given input source (XSL) as an XML document and returning DOM object";
throw new MapperException(MapperErro rCode.MAPP ER_MAP_INI T_XSL, this.getClass(), e, msg, "The XSLPath (stylesheet) : " + this.xslMappingFile, "Check XSL being well-formed");
}
catch (IOException e) {
String msg = "Path to the transforming stylesheet (XSL) is not correct or does not exist";
throw new MapperException(MapperErro rCode.MAPP ER_MAP_INI T_XSL, this.getClass(), e, msg, "The XSLPath (stylesheet) : " + this.xslMappingFile);
}
DOMSource xslDomSource = new DOMSource(xslDoc);
// creating XML Document for input
Document xmlDoc = null;
try {
File fs = new File(this.inputFile);
logger.debug("fs.getName() .."+fs.get Name());
logger.debug("input file..."+this.inputFile);
xmlDoc = dBuilder.parse(fs);
logger.debug("xmlDoc...."+ getDocumen tAsString( xmlDoc));
}
catch (SAXException e) {
String msg = "Error parsing the given input source (XML) as an XML document and returning DOM object";
throw new MapperException(MapperErro rCode.MAPP ER_MAP_INI T_XML, this.getClass(), e, msg, "The XMLPath (inputfile) : " + this.inputFile, "Check XML being well-formed");
}
catch (IOException e) {
String msg = "Path to the inputfile (XML) is not correct or does not exist";
logger.debug("Got IOException ...");
throw new MapperException(MapperErro rCode.MAPP ER_MAP_INI T_XML, this.getClass(), e, msg, "The XMLPath (inputfile) : " + this.inputFile);
}
DOMSource xmlDomSource = new DOMSource(xmlDoc);
// creating TransformerFactory
Transformer transformer = null;
try {
transformer = TransformerFactory.newInst ance().new Transforme r(xslDomSo urce);
}
catch (TransformerConfigurationE xception e) {
String msg = "A serious configuration error has occured, Transformer cannot be created satisfying the configuration requested";
throw new MapperException(MapperErro rCode.MAPP ER_MAP_TRA NSFORMER_I NIT, this.getClass(), e, msg, "The DOMSource (XSL) NodeName : " + xslDomSource.getNode().get NodeName() ); // + "ColumnNumber : " + e.getLocator().getColumnNu mber() + "LineNumber : " + e.getLocator().getLineNumb er());
}
catch (TransformerFactoryConfigu rationErro r transformerFactoryConfigur ationError ) {
String msg = "This class of the transformation factory specified in the system properties cannot be found or instantiated";
throw new MapperException(MapperErro rCode.MAPP ER_MAP_TRA NSFORMER_I NIT, this.getClass(), transformerFactoryConfigur ationError , msg, "The DOMSource (XSL) NodeName : " + xslDomSource.getNode().get NodeName() );
}
the output xml contains <?xml version="1.0" encoding="UTF-8"?> while I want to put it as <?xml version="1.0" encoding="ISO-8859-2"?>
logger.debug("Executing map");
logger.debug("mapping parameters" + params.toString());
String fileExtension = null;
this.xslMappingFile = params.getXslMappingFile()
this.inputFile = params.getInputFile();
logger.debug("this.inputFi
String fileType = "";
switch (params.getFileType()) {
case MapperParams.WIT : {
fileType="_WIT";
fileExtension = ".xml";
break;
}
case MapperParams.MIB : {
fileType="_MIB";
fileExtension = ".txt";
break;
}
default :
break;
}
this.witXmlFile = inputFile + fileType + fileExtension;
logger.debug("the new output "+fileType+" XMLFile " + this.witXmlFile);
DocumentBuilderFactory dFactory = DocumentBuilderFactory.new
dFactory.setNamespaceAware
// creating DocumentBuilder
DocumentBuilder dBuilder = null;
try {
dBuilder = dFactory.newDocumentBuilde
}
catch (ParserConfigurationExcept
String msg = "DocumentBuilder cannot be created satisfying the configuration requested";
throw new MapperException(MapperErro
}
// creating XSL Document for mapping
Document xslDoc = null;
try {
xslDoc = dBuilder.parse(FileLoading
}
catch (SAXException e) {
String msg = "Error parsing the given input source (XSL) as an XML document and returning DOM object";
throw new MapperException(MapperErro
}
catch (IOException e) {
String msg = "Path to the transforming stylesheet (XSL) is not correct or does not exist";
throw new MapperException(MapperErro
}
DOMSource xslDomSource = new DOMSource(xslDoc);
// creating XML Document for input
Document xmlDoc = null;
try {
File fs = new File(this.inputFile);
logger.debug("fs.getName()
logger.debug("input file..."+this.inputFile);
xmlDoc = dBuilder.parse(fs);
logger.debug("xmlDoc...."+
}
catch (SAXException e) {
String msg = "Error parsing the given input source (XML) as an XML document and returning DOM object";
throw new MapperException(MapperErro
}
catch (IOException e) {
String msg = "Path to the inputfile (XML) is not correct or does not exist";
logger.debug("Got IOException ...");
throw new MapperException(MapperErro
}
DOMSource xmlDomSource = new DOMSource(xmlDoc);
// creating TransformerFactory
Transformer transformer = null;
try {
transformer = TransformerFactory.newInst
}
catch (TransformerConfigurationE
String msg = "A serious configuration error has occured, Transformer cannot be created satisfying the configuration requested";
throw new MapperException(MapperErro
}
catch (TransformerFactoryConfigu
String msg = "This class of the transformation factory specified in the system properties cannot be found or instantiated";
throw new MapperException(MapperErro
}
the output xml contains <?xml version="1.0" encoding="UTF-8"?> while I want to put it as <?xml version="1.0" encoding="ISO-8859-2"?>
Have a go with this. If it doesn't work, try tweaking the encoding string:
transformer.setOutputPrope rty(javax. xml.transf orm.Output Keys.ENCOD ING, "ISO8859-2");
transformer.setOutputPrope
ASKER
Still same output I mean in the resulting xml file <?xml version="1.0" encoding="UTF-8"?> is coming not <?xml version="1.0" encoding="ISO-8859-2"?>
You're setting it immediately after this i assume?:
>>transformer = TransformerFactory.newInst ance().new Transforme r(xslDomSo urce);
>>transformer = TransformerFactory.newInst
ASKER
yes
try {
transformer = TransformerFactory.newInst ance().new Transforme r(xslDomSo urce);
logger.debug("b4********** ********** ********** ******tran sformer.se tOutputPro perty");
transformer.setOutputPrope rty(javax. xml.transf orm.Output Keys.ENCOD ING, "ISO8859-2");
}
try {
transformer = TransformerFactory.newInst
logger.debug("b4**********
transformer.setOutputPrope
}
Try iterating the properties before and after.
ASKER
sorry could not get you.
ASKER
During xmlDoc = dBuilder.parse(fs); the xmlDoc contains <?xml version="1.0" encoding="UTF-8"?> while in the fs it is <?xml version="1.0" encoding="ISO-8859-2"?> .
I think if I can change here then it may give the correct result but how I can change do not know.
I think if I can change here then it may give the correct result but how I can change do not know.
Presumably you're transforming using http://java.sun.com/j2se/1.4.1/docs/api/javax/xml/transform/Transformer.html#transform(javax.xml.transform.Source, javax.xml.transform.Result )
Try setting the encoding in the Result
Try setting the encoding in the Result
ASKER
Thanks a Lot for your responses.
Yes the code is
DOMResult result = new DOMResult();
try {
logger.debug("xmlDomSource ..."+xmlDo mSource.to String());
logger.debug("result..."+r esult.toSt ring());
try{
logger.debug("result.getNo de()..."+g etDocument AsString(( Document)r esult.getN ode()));
}catch(Exception e)
{
logger.debug("Exception in result.getNode()...");
}
transformer.transform(xmlD omSource, result);
}
but how I can set the encoding in the result. there is no method for this it seems.
Yes the code is
DOMResult result = new DOMResult();
try {
logger.debug("xmlDomSource
logger.debug("result..."+r
try{
logger.debug("result.getNo
}catch(Exception e)
{
logger.debug("Exception in result.getNode()...");
}
transformer.transform(xmlD
}
but how I can set the encoding in the result. there is no method for this it seems.
That's probably because you're using a DOMResult - until things are moved out of memory and into a renderable form, encoding is not really an issue. If you were to use a StreamResult, you could set the encoding.
ASKER
even in StreamResult there is no such method for changing the encoding
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileNameO ut), "ISO8859_2")));
StreamResult out = new StreamResult(br);
StreamResult out = new StreamResult(br);
ASKER
Yes this finally I am doing but my main point is that the output file should have the prolog of <?xml version="1.0" encoding="ISO-8859-2"?> not <?xml version="1.0" encoding="UTF-8"?> . This what you wrote will give the output charset as ISO... but not what I need in the prolog. My requirement is to further read the resulting xml & then do something but as the resulting xml have ISO.. char set & the prolog have UTF-8 so I get the exception.
OK - got you. What about setting the prolog using the xsl?
ASKER
<?xml version="1.0" encoding="ISO-8859-2"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-2" indent="yes"/>
<xsl:template match="/">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="ISO-8859-2" indent="yes"/>
<xsl:template match="/">
ASKER
I am using the above prolog. Though the XSLT I have written have all english character's but still I used <?xml version="1.0" encoding="ISO-8859-2"?>
but no use since getting the exception.
but no use since getting the exception.
Is that not working then?
ASKER
yes it is not working..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
//read
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileNameIn
// write
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileNameO