Hi,

My java program rarely(1 out of 1000 times) writes a corrupt xml file. corrupt xml file doesnt have tags from start but somewhere from mid. also first tag written in xml file doesnt have character.

and it happens very rarely. in successful cases, the same code writes correct xml.

following code is used to write xml. application is running on linux based Set top box and writing xml file on flash (STB doesnt have Hard disk). could writing on flash a reason of such problems?

Java Code:
private void storeXML(Channel[] channels) {

    	if (logger.isDebugEnabled()) {
    		logger.debug("+ storeXML ");
    	}

        File tempFile = new File(SUBSCRIBED_DTV_LINEUP_FILE_PATH_TEMP);
        File subscriberDTVXMLFile = new File(SUBSCRIBED_DTV_LINEUP_FILE_PATH);

        if(channels.length == 0) {

            if(subscriberDTVXMLFile.exists()) {

                if(subscriberDTVXMLFile.delete()) {
					if (logger.isDebugEnabled()) {
						logger.debug("Delete done");
					}
                }
                else {
					if (logger.isLoggerEnabled()) {
						logger.warn("problem while deleting file.");
					}
                }
            }

            return;
        }

        StringBuffer buf = new StringBuffer(1000);
        XMLUtil.generateElementStart(buf, MessageTypes.CHANNEL_LIST);
        Channel channel;
        boolean dvbChannel;
        int[][] idTriplets;
        int[] currentRow;

        for (int i = 0; i   channels.length; i++) {

            channel = channels;
            //todo TAG_DVBTCHANNEL if channel dvbt
            dvbChannel = channel.isType(DVBChannel.CHTYPE_DVB);

            if(dvbChannel) {

                XMLUtil.generateElementStart(buf, MessageTypes.TAG_DVBTCHANNEL);
                idTriplets = ((DVBChannel) channel).getIDTripletsArray();

                for(int rowIdx = 0; rowIdx  idTriplets.length; rowIdx++) {
                    currentRow = idTriplets[rowIdx];
                    XMLUtil.generateElementStart(buf, MessageTypes.TAG_SERVICE);
                    XMLUtil.generateElement(buf, MessageTypes.TAG_SERVICEID, currentRow[0]); // = serviceID;
                    XMLUtil.generateElement(buf, MessageTypes.TAG_TRANSPORTID, currentRow[1]); // = transportID;
                    XMLUtil.generateElement(buf, MessageTypes.TAG_ORIGNETWORKID, currentRow[2]); // = originalNetworkID;
                    XMLUtil.generateElementEnd(buf, MessageTypes.TAG_SERVICE);
                }
            }
            else {
                XMLUtil.generateElementStart(buf, MessageTypes.TAG_CHANNEL);
            }

            XMLUtil.generateElement(buf, MessageTypes.TAG_CH_ID, channel.getChannelID());
            XMLUtil.generateElement(buf, MessageTypes.TAG_CHANNEL_REFERENCE_NUMBER, channel.getChannelReferenceNumber());
            XMLUtil.generateElement(buf, MessageTypes.TAG_CH_DISPLAYNUMBER, channel.getVisibleNumber());
            XMLUtil.generateElement(buf, MessageTypes.TAG_CH_CALLLETTER, channel.getCallsign());
            XMLUtil.generateElement(buf, MessageTypes.TAG_CH_TYPE, channel.getTypeStr());
            XMLUtil.generateElement(buf, MessageTypes.TAG_CH_IP, channel.getIp());
            XMLUtil.generateElement(buf, MessageTypes.TAG_CH_PORT, channel.getPort());

            if(channel.getM_VideoTransport() != null) {
            	XMLUtil.generateElement(buf, MessageTypes.TAG_CH_VIDEOTRANSPORT, channel.getM_VideoTransport());
            }

            XMLUtil.generateElement(buf, MessageTypes.TAG_HD_RESOLUTION, channel.isHDContents() ? "HD" : "SD");

            /*todo save below information for AVR
               subscriber ID = persistentdatamanager.getSubscriberID(),
               Viewer ID = ProfileManager.getViewerID() ,
               STB Name=ProfileManager.getSTBName()
            */
            if (channel.isFirstTunedChannel()) {
            	XMLUtil.generateElement(buf, MessageTypes.TAG_CH_FIRST_TUNED_CHANNEL, true);
            }

            if(dvbChannel) {
                XMLUtil.generateElementEnd(buf, MessageTypes.TAG_DVBTCHANNEL);
            }
            else {
                XMLUtil.generateElementEnd(buf, MessageTypes.TAG_CHANNEL);
            }
        }

        XMLUtil.generateElementEnd(buf, MessageTypes.CHANNEL_LIST);
        OutputStreamWriter osw = null;
        Exception exception = null;

        try {
            osw = new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8");

            try {
                osw.write(buf.toString());
            } catch (IOException e) {
                exception = e;
            }
        }

        catch (FileNotFoundException e) {
            exception = e;
        }
        catch (UnsupportedEncodingException e) {
            exception = e;
        }

        finally {
            if (osw != null) {

                try {
                    osw.close();
                } catch (IOException e) {
                    exception = e;
                }
            }
        }

        if(exception == null) { // every thing is fine, replace the file with orignal name.

            if(subscriberDTVXMLFile.exists()) {
                subscriberDTVXMLFile.delete();
            }

            boolean renamed = tempFile.renameTo(subscriberDTVXMLFile);

            if(logger.isInfoEnabled()) {
                logger.info("File rename successfully? " + renamed);
            }
        }
        else {
			if (logger.isLoggerEnabled()) {
				logger.error("exception was occured:");
			}
			exception.printStackTrace();
        }

        if (logger.isDebugEnabled()) {
        	logger.debug("- storeXML");
        }
    }



it is urgent. please suggest a proper solution.

rashmi