I have my existing code to Java Mapping to convert a text file to an XML file.
This works perfectly fine. Please find the code below.
I get few records in the converted XML file and neeed to post one IDOC for every record in the XML file.
I use multi-mapping to post one IDOC for every XML record.
My Java Program output is :
<?xml version="1.0" encoding="UTF-8" standalone="no"?><MT_CostInterfaceFromLegacy><EMPLOYEES><CURRENCY>USD</CURRENCY><DATE>022912</DATE><COSTNUMBER>00010419-000001-022C</COSTNUMBER><T1>-</T1><PRICE>15.50000000</PRICE><SNO>1001</SNO><T2>-</T2><T3>-</T3><NUMBER>022C</NUMBER><T4>-</T4><T5>-</T5><T6>-</T6><T7>-</T7><T8>-</T8><DESC1>charges made by Woo, Wei Kwang total number of copies = 1 on number 008613506201605 at 15:13 </DESC1></EMPLOYEES><EMPLOYEES><CURRENCY>USD</CURRENCY><DATE>08/01/2011</DATE><COSTNUMBER>00010419-000001-022C</COSTNUMBER><T1>-</T1><PRICE>34.50000000</PRICE><SNO>1001</SNO><T2>-</T2><T3>-</T3><NUMBER>022C</NUMBER><T4>-</T4><T5>-</T5><T6>-</T6><T7>-</T7><T8>-</T8><DESC1>charges made by Woo, Wei Kwang total number of copies = 1 on number 0169184586 at 15:23 </DESC1></EMPLOYEES><EMPLOYEES><CURRENCY>USD</CURRENCY><DATE>022912</DATE><COSTNUMBER>00010419-000001-022C</COSTNUMBER><T1>-</T1><PRICE>52.80000000</PRICE><SNO>1001</SNO><T2>-</T2><T3>-</T3><NUMBER>022C</NUMBER><T4>-</T4><T5>-</T5><T6>-</T6><T7>-</T7><T8>-</T8><DESC1>charges made by Woo, Wei Kwang total number of copies = 1 on number 008613506201605 at 17:40 </DESC1></EMPLOYEES></MT_CostInterfaceFromLegacy>
I need the output with the following tags added :
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<EMPLOYEES>
.....
.....
</EMPLOYEES>
</ns0:Message1>
</ns0:Messages>
My Java Mapping Code is ::
package xmlFiles;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class CreateXML1 {
public static final String xmlFilePath = "C:\\Temp\\Emp.xml";
public static void main(String[] args) throws Exception {
CreateXML1 createXML = new CreateXML1();
createXML.readCostFile();
}
public void readCostFile() throws Exception{
BufferedReader br = new BufferedReader(new FileReader(new File("C:\\Temp\\Time.txt")));
String inputLine = null;
ArrayList<String> records = new ArrayList<String>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.newDocument();
Element empsRootEle = doc.createElement("MT_CostInterfaceFromLegacy");
int count = 0;
while((inputLine = br.readLine()) != null ){
if (inputLine.equals("")){
System.out.println("This is my carraige return " + inputLine + count);
// Parse XMl file to get the records
Element empEle = doc.createElement("EMPLOYEES");
Element dateworked = doc.createElement("DATEWORKED");
Element timekeeperglobalid = doc.createElement("TIMEKEEPERGLOBALID");
Element clientid = doc.createElement("CLIENTID");
Element matter = doc.createElement("MATTER");
Element physicalallocation = doc.createElement("PHYSICALALLOCATION");
Element hoursworked = doc.createElement("HOURSWORKED");
Element ffactcode = doc.createElement("FFACTCODE");
Element activitycode = doc.createElement("ACTIVITYCODE");
Element fftaskcode = doc.createElement("FFTASKCODE");
Element taskcode = doc.createElement("TASKCODE");
Element areaoflaw = doc.createElement("AREAOFLAW");
Element matterloc = doc.createElement("MATTERLOC");
Element segment = doc.createElement("SEGMENT");
Element description = doc.createElement("DESCRIPTION");
Text currentText = null; Text dateText = null; Text costNumberText = null;
Text indexText = null; Text t1Text = null; Text priceText = null;
Text sNoText = null; Text t2Text = null; Text t3Text = null;
Text numberText = null; Text t4Text = null; Text t5Text = null;
Text t6Text = null;
Text desc1Text = null;
if(records.size()>12){
currentText = doc.createTextNode(records.get(0));
dateText = doc.createTextNode(records.get(1));
costNumberText = doc.createTextNode(records.get(2));
indexText = doc.createTextNode(records.get(3));
t1Text = doc.createTextNode(records.get(4));
priceText = doc.createTextNode(records.get(5));
sNoText = doc.createTextNode(records.get(6));
t2Text = doc.createTextNode(records.get(7));
t3Text = doc.createTextNode(records.get(8));
numberText = doc.createTextNode(records.get(9));
t4Text = doc.createTextNode(records.get(10));
t5Text = doc.createTextNode(records.get(11));
t6Text = doc.createTextNode(records.get(12));
String S = "";
for (int j = 13; j<records.size(); j++){
S = S + records.get(j) + " ";
}
S = S.replaceAll("[&#]", "");
desc1Text = doc.createTextNode(S);
} else if (records.size()<=12){
currentText = doc.createTextNode(records.get(0));
dateText = doc.createTextNode(records.get(1));
costNumberText = doc.createTextNode(records.get(2));
indexText = doc.createTextNode(records.get(3));
t1Text = doc.createTextNode(records.get(4));
priceText = doc.createTextNode(records.get(5));
sNoText = doc.createTextNode(records.get(6));
t2Text = doc.createTextNode(records.get(7));
t3Text = doc.createTextNode(records.get(8));
numberText = doc.createTextNode(records.get(9));
t4Text = doc.createTextNode(records.get(10));
t5Text = doc.createTextNode(records.get(11));
t6Text = doc.createTextNode(records.get(12));
}
dateworked.appendChild(currentText);
timekeeperglobalid.appendChild(dateText);
clientid.appendChild(costNumberText);
matter.appendChild(indexText);
physicalallocation.appendChild(t1Text);
hoursworked.appendChild(priceText);
ffactcode.appendChild(sNoText);
activitycode.appendChild(t2Text);
fftaskcode.appendChild(t3Text);
taskcode.appendChild(numberText);
areaoflaw.appendChild(t4Text);
matterloc.appendChild(t5Text);
segment.appendChild(t6Text);
description.appendChild(desc1Text);
empEle.appendChild(dateworked);
empEle.appendChild(timekeeperglobalid);
empEle.appendChild(clientid);
empEle.appendChild(matter);
empEle.appendChild(physicalallocation);
empEle.appendChild(hoursworked);
empEle.appendChild(ffactcode);
empEle.appendChild(activitycode);
empEle.appendChild(fftaskcode);
empEle.appendChild(taskcode);
empEle.appendChild(areaoflaw);
empEle.appendChild(matterloc);
empEle.appendChild(segment);
empEle.appendChild(description);
empsRootEle.appendChild(empEle);
records.clear();
count++;
} else {
records.add(inputLine);
}
//System.out.println("This is not my carraige return" + inputLine);
}
if(records.iterator().hasNext()){
Element empEle = doc.createElement("EMPLOYEES");
Element dateworked = doc.createElement("DATEWORKED");
Element timekeeperglobalid = doc.createElement("TIMEKEEPERGLOBALID");
Element clientid = doc.createElement("CLIENTID");
Element matter = doc.createElement("MATTER");
Element physicalallocation = doc.createElement("PHYSICALALLOCATION");
Element hoursworked = doc.createElement("HOURSWORKED");
Element ffactcode = doc.createElement("FFACTCODE");
Element activitycode = doc.createElement("ACTIVITYCODE");
Element fftaskcode = doc.createElement("FFTASKCODE");
Element taskcode = doc.createElement("TASKCODE");
Element areaoflaw = doc.createElement("AREAOFLAW");
Element matterloc = doc.createElement("MATTERLOC");
Element segment = doc.createElement("SEGMENT");
Element description = doc.createElement("DESCRIPTION");
Text currentText = null; Text dateText = null; Text costNumberText = null;
Text indexText = null; Text t1Text = null; Text priceText = null;
Text sNoText = null; Text t2Text = null; Text t3Text = null;
Text numberText = null; Text t4Text = null; Text t5Text = null;
Text t6Text = null;
Text desc1Text = null;
if(records.size()>12){
currentText = doc.createTextNode(records.get(0));
dateText = doc.createTextNode(records.get(1));
costNumberText = doc.createTextNode(records.get(2));
indexText = doc.createTextNode(records.get(3));
t1Text = doc.createTextNode(records.get(4));
priceText = doc.createTextNode(records.get(5));
sNoText = doc.createTextNode(records.get(6));
t2Text = doc.createTextNode(records.get(7));
t3Text = doc.createTextNode(records.get(8));
numberText = doc.createTextNode(records.get(9));
t4Text = doc.createTextNode(records.get(10));
t5Text = doc.createTextNode(records.get(11));
t6Text = doc.createTextNode(records.get(12));
String S = "";
for (int j = 13; j<records.size(); j++){
S = S + records.get(j) + " ";
}
S = S.replaceAll("[&#]", "");
desc1Text = doc.createTextNode(S);
} else if (records.size()<=12){
currentText = doc.createTextNode(records.get(0));
dateText = doc.createTextNode(records.get(1));
costNumberText = doc.createTextNode(records.get(2));
indexText = doc.createTextNode(records.get(3));
t1Text = doc.createTextNode(records.get(4));
priceText = doc.createTextNode(records.get(5));
sNoText = doc.createTextNode(records.get(6));
t2Text = doc.createTextNode(records.get(7));
t3Text = doc.createTextNode(records.get(8));
numberText = doc.createTextNode(records.get(9));
t4Text = doc.createTextNode(records.get(10));
t5Text = doc.createTextNode(records.get(11));
t6Text = doc.createTextNode(records.get(12));
}
dateworked.appendChild(currentText);
timekeeperglobalid.appendChild(dateText);
clientid.appendChild(costNumberText);
matter.appendChild(indexText);
physicalallocation.appendChild(t1Text);
hoursworked.appendChild(priceText);
ffactcode.appendChild(sNoText);
activitycode.appendChild(t2Text);
fftaskcode.appendChild(t3Text);
taskcode.appendChild(numberText);
areaoflaw.appendChild(t4Text);
matterloc.appendChild(t5Text);
segment.appendChild(t6Text);
description.appendChild(desc1Text);
empEle.appendChild(dateworked);
empEle.appendChild(timekeeperglobalid);
empEle.appendChild(clientid);
empEle.appendChild(matter);
empEle.appendChild(physicalallocation);
empEle.appendChild(hoursworked);
empEle.appendChild(ffactcode);
empEle.appendChild(activitycode);
empEle.appendChild(fftaskcode);
empEle.appendChild(taskcode);
empEle.appendChild(areaoflaw);
empEle.appendChild(matterloc);
empEle.appendChild(segment);
empEle.appendChild(description);
empsRootEle.appendChild(empEle);
}
doc.appendChild(empsRootEle);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
DOMSource docObj = new DOMSource(doc);
StreamResult sr = new StreamResult(new FileOutputStream(xmlFilePath));
t.transform(docObj, sr);
System.out.println("XML File is created");
}
}