How to use Java SOAPElement
1. javax.xml.soap
à
saaj.jar
Interface SOAPElement
http://java.sun.com/j2ee/1.4/docs/api/javax/xml/soap/SOAPElement.html
This package is defined in the SOAP with Attachments API for JavaTM (SAAJ) 1.1 specification.
SOAPElement could be used as Element
Because
public interface SOAPElement
extends Node, Element
2. Using xsd:anyType to Represent XML Documents in a Service Interface: Design Details
https://bpcatalog.dev.java.net/nonav/soa/doc-anytype/design.html
public class PurchaseOrder {
private String poId;
private Calendar createDate;
private Address shipTo;
private Address billTo;
private LineItem[] items;
public PurchaseOrder() {}
public PurchaseOrder(String poId, Calendar createDate,
Address shipTo, Address billTo, LineItem[] items) {
this.poId = poId;
this.shipTo = shipTo;
this.createDate = createDate;
this.billTo = billTo;
this.items = items;
}
...
public SOAPElement toXMLSOAPElement(boolean wrapper) {
SOAPElement soapElem = null;
try {
//construct the DOM tree
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element poElem = doc.createElement("PurchaseOrder");
if(wrapper){
Element docElem = doc.createElement("BusinessDocumentRequest");
doc.appendChild(docElem);
docElem.appendChild(poElem);
} else{
doc.appendChild(poElem);
}
Element elem = doc.createElement("poId");
elem.appendChild(doc.createTextNode(poId));
poElem.appendChild(elem);
elem = doc.createElement("createDate");
elem.appendChild(doc.createTextNode((new SimpleDateFormat("MM-dd-yy")).format(createDate.getTime())));
poElem.appendChild(elem);
elem = doc.createElement("shipTo");
poElem.appendChild(shipTo.toDOM(doc, elem));
elem = doc.createElement("billTo");
poElem.appendChild(billTo.toDOM(doc, elem));
for(int i = 0; i < items.length; ++i){
poElem.appendChild(items[i].toDOM(doc));
}
//create a SOAPElement
SOAPElement parent = SOAPFactory.newInstance().createElement("dummy");
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(new DOMSource(doc), new DOMResult(parent));
soapElem = (SOAPElement) parent.getChildElements().next();
} catch(TransformerException te) {
te.printStackTrace(System.err);
throw new RuntimeException(te.getMessage(), te);
} catch(ParserConfigurationException pce) {
pce.printStackTrace(System.err);
throw new RuntimeException(pce.getMessage(), pce);
} catch(SOAPException se) {
se.printStackTrace(System.err);
throw new RuntimeException(se.getMessage(), se);
}
return soapElem;
}