I have a requirement where the text file looks like as shown below
XXX Czech Republic s.r.o.
Xe XX? 559
252 67 XXX??ice
Phone: +000 000 003 300
========================================= Remittance Advice ==============================================================
xxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
CZ
==========================================================================================================================
Attached is a cheque in respect of the 3 COD(s) for the shipments noted below.
Tracking Number Recipient Reference Cod Amount Remittance
13699302850434 xxxxxxxxxxxxxxxxxx CZK 17322.00 CZK 17322.00
xxxxxxxxxxxxx
13699302969163 xxxxxx EUR 1424.00 CZK 1424.00
xxxxxxxxxxxxxx
13699302965778 xxxxxxxxxx EUR 4720.00 CZK 4720.00
xxxxxxxxxx
____________
Total remitted 23466.00
============
I have to read the data beneath those header columns and fetch some details from DB using that info and finally convert into xml.
I am no expert in Java so I came up with below logic but all it does is split the data but how do I read through individual columns.
private static void processTextFile(File attachment, Date emailDate) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(attachment)));
String line = null;
String[] headers = null;
String[] data = null;
Map<String, List<String>> contents = new HashMap<String, List<String>>();
if ((line = in.readLine()) != null) {
headers = line.split("t");
}
for(String h : headers){
contents.put(h, new ArrayList<String>());
}
while ((line = in.readLine()) != null && line.startsWith("Attached is a cheque")) {
data = line.split("t");
if(data.length != headers.length){
throw new Exception();
}
for(int i = 0; i < data.length; i++){
contents.get(headers[i]).add(data[i]);
}
}
}
Output XML format
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CODRemittance>
<ExternalDocNo>filename</ExternalDocNo>
<DocumentDate>YYYYMDD</DocumentDate>
<Invoicee>XXX Czech Republic s.r.o.</Invoicee>
<InvoiceLine>
<TrackingNo>1Z763W8Y7991338730</TrackingNo>
<Recipient>MICXXXXLES EE</Recipient>
<Reference>24011921</Reference>
<LineNo>1</LineNo>
<CODCurrencyCode>EUR</CODCurrencyCode>
<CODAmount>1337.01</COD_Amount>
<RemitCurrencyCode>CZK</RemitCurrencyCode>
<RemitAmount>33317.09</RemitAmount>
<CustomerID>37162537</CustomerID>
<InvoiceNo>10084189</InvoiceNo>
</SalesInvoiceLine>
<Total>
<RemitTotal>33317.09</RemitTotal>
<TotalNoOfLines>1</TotalNoOfLines>
</Total>
</CODRemittance>
I created two BO classes to match the above output xml format
public class CODTextFileBO {
private String trackingNumber;
private String recipient;
private String reference;
private Float codAmount;
private String currencyCode;
public String getTrackingNumber() {
return trackingNumber;
}
public void setTrackingNumber(String trackingNumber) {
this.trackingNumber = trackingNumber;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public Float getCodAmount() {
return codAmount;
}
public void setCodAmount(Float codAmount) {
this.codAmount = codAmount;
}
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String currencyCode) {
this.currencyCode = currencyCode;
}
}
public class DataFromDBTableBO {
private String accountNumber;
private String invoiceNumber;
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public String getInvoiceNumber() {
return invoiceNumber;
}
public void setInvoiceNumber(String invoiceNumber) {
this.invoiceNumber = invoiceNumber;
}
}
How do I read through the text file where the data starts from somewhere in between and how do I convert it into xml. Any suggestions are appreciated. TIA!