public PurchaseOrderDocument createDocument(PurchaseOrderDocument poDoc,
String file) {
// Get object reference of root element.
PurchaseOrderDocument.PurchaseOrder purchaseOrderElement = poDoc.getPurchaseOrder();
InvoiceHeaderDocument.InvoiceHeader invHeaders = purchaseOrderElement.addNewInvoiceHeader();
// Assign values to the newly created invoice-header element.
NameAddress shipto = invHeaders.addNewShipTo();
shipto.setName("New Company");
shipto.setAddress("NewTown, NewCity");
NameAddress billto = invHeaders.addNewBillTo();
billto.setName("New Company");
billto.setAddress("NewTown, NewCity");
// Create a new Book and add it to the invoice.
BookType book = BookType.Factory.newInstance();
book.setId(1000);
book.setTitle("Where the Red Fern Grows");
invHeaders.setProduct(book);
XmlCursor cursor = invHeaders.getProduct().newCursor();
cursor.setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "book"));
cursor.dispose();
// Creating a new comment - with substitution group member bill-comment element.
invHeaders.setComment("This is a new bill-comment");
cursor = invHeaders.xgetComment().newCursor();
cursor.setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "bill-comment"));
cursor.dispose();
// Add another invoice-header.
invHeaders = purchaseOrderElement.addNewInvoiceHeader();
// Assign values to the newly created invoice-header element.
shipto = invHeaders.addNewShipTo();
shipto.setName("Other Company");
shipto.setAddress("OtherTown, OtherCity");
billto = invHeaders.addNewBillTo();
billto.setName("Other Company");
billto.setAddress("OtherTown, OtherCity");
// Create a new Clothing and add it to the invoice.
ClothingType clothing = ClothingType.Factory.newInstance();
clothing.setId(2000);
clothing.setColor(ClothingType.Color.BLUE);
invHeaders.setProduct(clothing);
cursor = invHeaders.getProduct().newCursor();
cursor.setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "clothing"));
cursor.dispose();
// Creating a new comment - with substitution group member bill-comment element.
invHeaders.setComment("This is a new bill-comment");
cursor = invHeaders.xgetComment().newCursor();
cursor.setName(new QName("http://xmlbeans.apache.org/samples/substitutiongroup/easypo", "ship-comment"));
cursor.dispose();
// Validate it.
validateXml(poDoc);
XmlOptions xmlOptions = new XmlOptions();
xmlOptions.setSavePrettyPrint();
File f = new File(file);
try
{
//Writing the XML Instance to a file.
poDoc.save(f,xmlOptions);
}
catch(IOException e)
{
e.printStackTrace();
}
System.out.println("\n\n\nXML Instance Document saved at : " + f.getPath());
return poDoc;
}
This method creates an new invoice-header element and attaches to the existing XML Instance, and saves the
new Instance to a file(args[1]). |