Link to home
Start Free TrialLog in
Avatar of vikas31
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.MalformedInputException.

Vikas
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Try using the following pattern:


//read


BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileNameIn), "ISO8859_2")));

// write

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileNameOut), "ISO8859_2")));
Avatar of vikas31
vikas31

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?
Avatar of vikas31

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.inputFile....."+inputFile);
      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.newInstance();
      dFactory.setNamespaceAware(true);

         // creating DocumentBuilder
      DocumentBuilder dBuilder = null;
      try {
         dBuilder = dFactory.newDocumentBuilder();
      }
      catch (ParserConfigurationException e) {
         String msg = "DocumentBuilder cannot be created satisfying the configuration requested";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT, this.getClass(), e, msg, "domfactory.newDocumentBuilder() | DocumentBuilder cannot be created");
      }

      // creating XSL Document for mapping
      Document xslDoc = null;
      try {
         xslDoc = dBuilder.parse(FileLoadingUtils.getResourceAsInputStream(xslMappingFile));
      }
      catch (SAXException e) {
         String msg = "Error parsing the given input source (XSL) as an XML document and returning DOM object";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT_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(MapperErrorCode.MAPPER_MAP_INIT_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.getName());
        logger.debug("input file..."+this.inputFile);
         xmlDoc = dBuilder.parse(fs);
            logger.debug("xmlDoc...."+getDocumentAsString(xmlDoc));
      }
      catch (SAXException e) {
         String msg = "Error parsing the given input source (XML) as an XML document and returning DOM object";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_INIT_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(MapperErrorCode.MAPPER_MAP_INIT_XML, this.getClass(), e, msg, "The XMLPath (inputfile)    : " + this.inputFile);
      }
      DOMSource xmlDomSource = new DOMSource(xmlDoc);

      // creating TransformerFactory
      Transformer transformer = null;
      try {
         transformer = TransformerFactory.newInstance().newTransformer(xslDomSource);
      }
      catch (TransformerConfigurationException e) {
         String msg = "A serious configuration error has occured, Transformer cannot be created satisfying the configuration requested";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_TRANSFORMER_INIT, this.getClass(), e, msg, "The DOMSource (XSL) NodeName   : " + xslDomSource.getNode().getNodeName()); // + "ColumnNumber : " + e.getLocator().getColumnNumber() + "LineNumber : " + e.getLocator().getLineNumber());
      }
      catch (TransformerFactoryConfigurationError transformerFactoryConfigurationError) {
         String msg = "This class of the transformation factory specified in the system properties cannot be found or instantiated";
         throw new MapperException(MapperErrorCode.MAPPER_MAP_TRANSFORMER_INIT, this.getClass(), transformerFactoryConfigurationError, msg, "The DOMSource (XSL) NodeName   : " + xslDomSource.getNode().getNodeName());
      }
  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.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "ISO8859-2");
Avatar of vikas31

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.newInstance().newTransformer(xslDomSource);
Avatar of vikas31

ASKER

yes

try {
         transformer = TransformerFactory.newInstance().newTransformer(xslDomSource);
            logger.debug("b4************************************transformer.setOutputProperty");
             transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "ISO8859-2");
      }
Try iterating the properties before and after.
Avatar of vikas31

ASKER

sorry could not get you.
Avatar of vikas31

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.
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
Avatar of vikas31

ASKER

Thanks  a Lot for your responses.

Yes the code is

DOMResult result = new DOMResult();
               try {
                        logger.debug("xmlDomSource..."+xmlDomSource.toString());
                        logger.debug("result..."+result.toString());
                        try{
                           logger.debug("result.getNode()..."+getDocumentAsString((Document)result.getNode()));
                     }catch(Exception e)
                     {
                        logger.debug("Exception in result.getNode()...");
                     }
                  transformer.transform(xmlDomSource, result);
               }
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.
Avatar of vikas31

ASKER

even in StreamResult there is no such method for changing the encoding
BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileNameOut), "ISO8859_2")));
StreamResult out = new StreamResult(br);
Avatar of vikas31

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?
Avatar of vikas31

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="/">
Avatar of vikas31

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.
Is that not working then?
Avatar of vikas31

ASKER

yes it is not working..
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial