I want to create XML File like the below - I have got it outputting fine to disk when I just create one OrderDetails child - Now I want to create 2 (so finally I could have an XML File with one batchheader and multiple order details on it.
<?xml version="1.0" encoding="UTF-8"?>
<BatchOrders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<BatchHeader>
<Provider>123456789</Provider>
<Contact>ABC@abc.com</Contact>
</BatchHeader>
<OrderDetails>
<Number>0456789</Number>
<YourReference>tc1</YourReference>
<DeliveryDate>23-08-2012</DeliveryDate>
<RetailerID>AAA</RetailerID>
</OrderDetails>
<OrderDetails>
<Number>1234</Number>
<YourReference>tc2</YourReference>
<DeliveryDate>23-08-2013</DeliveryDate>
<RetailerID>BBB</RetailerID>
</OrderDetails>
</BatchOrders>
So I have a class called OrderDetails that has the propeties and the get/setters. I also have a class that writes the XML Header and then the OrderDetails - this is below.
OrderHeader header = new OrderHeader();
header.setProvider("123456789");
//rest of header setting done
OrderWriter writer = new OrderWriter(orderWriter);
writer.Initialise(header);
OrderDetails[] orderdetails = new OrderDetails[3];
for(int i = 0; i < orderdetails.length; i++)
{
orderdetails[i] = new OrderDetails();
orderdetails[i].setDirectoryNumber("0456789" + i);
//rest of sets done
writer.writeNext(orderdetails[i]);
}
writer.close();
Finally below is my writer class - when I have only one order details it works fine and prints the file in correct XML format. When I try the code which creates multi OrderDetails child nodes - it just gives me the first order details recoard and in one long string of text so not correctly formatted XML - can anyone see something silly I am missing here?
public void Initialise(OrderHeader header) throws Exception
{
Element batchOrders = new Element("BatchOrders");
document.setRootElement(batchOrders);
Element batchHeader = new Element("BatchHeader");
batchHeader.addContent(new Element("ServiceProvider").setText(header.getServiceProvider()));
//more header sets are done...
document.getRootElement().addContent(batchHeader);
}
public void close() throws Exception { writer.close(); }
public void writeNext(OrderDetails record) throws Exception
{
Element orderDetails = new Element("OrderDetails");
if(record.getNumber() != null)
{
orderDetails.addContent(new Element("Number").setText(record.getNumber()));
}
//More sets are done....
document.getRootElement().addContent(orderDetails);
outputter.getFormat().setOmitDeclaration(true);
outputter.getFormat().setOmitEncoding(true);
outputter.output(document,writer);
}