Number of records in XML File through structured Writer

Hi

I am using Structured Data Writer to write an xml file. In the file i have to write the information like filename, no of records etc in the header tag which will be written at the start of the xml file. The problem is although i can pass the number of records to this writer component, but i cant use a variable in the header. Is there any other way to write this info in header of an xml file.

Regrads

Hi,
problem is that the Structured Data Writer (and any other writer component as well) doesn’t know how many records it will get. Number of processed records is known after processing all records already. My solution is to count records in Reformat component. In its finish method write header of the file and with Structured Data Writer save only the body and footer :(.

Thanx for you rreply, although i understood what u meant, but can you please explain this finish method and writing header at the end thing in detail. Should it be done by java code that is written in reformat class or is there any functionality in ETL to this, i mean opening the same file and then writing header.

Thanx and Regards

I thought to write the header by java code in finish method; I don’t have better idea.
Graph:


<?xml version="1.0" encoding="UTF-8"?>
<Graph created="Wed Jan 09 08:56:52 CET 2008" guiVersion="1.8" id="1199880496440" licenseType="Evaluation license." modified="Thu Jan 10 08:57:42 CET 2008" name="test" revision="1.13">
<Global>
<Metadata id="Metadata0">
<Record name="defaultRecordName" recordSize="-1" type="delimited">
<Field delimiter="\n" name="Field1" nullable="true" shift="0" type="string"/>
</Record>
</Metadata>
</Global>
<Phase number="0">
<Node enabled="enabled" guiHeight="0" guiName="Data Generator" guiWidth="0" guiX="69" guiY="62" id="DATA_GENERATOR0" randomFields="Field1=random(&quot;2&quot;,&quot;6&quot;)" recordsNumber="100" type="DATA_GENERATOR"/>
<Node enabled="enabled" guiHeight="0" guiName="Reformat" guiWidth="0" guiX="284" guiY="63" id="REFORMAT0" transformClass="transformation" type="REFORMAT"/>
<Edge fromNode="DATA_GENERATOR0:0" guiBendpoints="" id="Edge0" inPort="Port 0 (in)" metadata="Metadata0" outPort="Port 0 (out)" toNode="REFORMAT0:0"/>
<Edge fromNode="REFORMAT0:0" guiBendpoints="" id="Edge1" inPort="Port 0 (Body port)" metadata="Metadata0" outPort="Port 0 (out)" toNode="STRUCTURE_WRITER0:0"/>
</Phase>
<Phase number="1">
<Node append="true" enabled="enabled" fileURL="out.txt" guiHeight="0" guiName="Structured Data Writer" guiWidth="0" guiX="477" guiY="58" id="STRUCTURE_WRITER0" mask="&lt;defaultRecordName&gt;
	&lt;Field1=$Field1/&gt;
&lt;defaultRecordName/&gt;
" type="STRUCTURE_WRITER"/>
</Phase>
</Graph>

transformation.java:


import java.io.File;
import java.io.FileOutputStream;

import org.jetel.component.DataRecordTransform;
import org.jetel.data.DataRecord;
import org.jetel.exception.TransformException;


public class transformation extends DataRecordTransform {
	
	int count = 0;

	@Override
	public boolean transform(DataRecord[] inputRecords,
			DataRecord[] outputRecords) throws TransformException {
		count++;
		outputRecords[0].copyFrom(inputRecords[0]);
		return true;
	}
	
	@Override
	public void finished() {
		FileOutputStream out;
		try {
			out = new FileOutputStream(new File("out.txt"));
			out.write((new String("<Records =" + count + ">\n")).getBytes());
			out.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.finished();
	}

}