I’m working on a webservices client app and I have it mostly working. I can retrieve and read data from the third-party webservice fine. Now I need to submit some data and I’m stuck.
The classes for the objects I’m retrieving/submitting were generated from XSD files via the xjc tool. The part I’m stuck on is turning one of those objects into an XML tree to submit to the webservice.
When I retrieve/send a request from/to the ws, it contains a ‘payload’ object. This is defined in java code as (partial listing):
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PayloadType", propOrder = {
"compressed",
"document",
"any",
"format"
})
public class PayloadType {
@XmlElement(name = "Compressed")
protected String compressed;
@XmlElement(name = "Document")
protected List<String> document;
@XmlAnyElement
protected List<Element> any;
protected String format;
public List<Element> getAny() {
if (any == null) {
any = new ArrayList<Element>();
}
return this.any;
}
}
The only field I’m concerned with is the ‘any’ field which contains an XML tree. When I retrieve data from the ws, I read that field with something like this:
(‘root’ is of org.w3c.dom.Element type and is the result of calling ‘getAny().get(0)’ on the payload object)
NodeList nl = root.getElementsByTagName("ns1:Process"); // "ns1:Process" is an XML node to do something with
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
Element proc = (Element) nl.item(i);
try {
// do something with the 'proc' Element here...
} catch (Exception ex) {
// handle problems here...
}
}
}
Submitting data is where I’m stuck. How do I take a java object created from one of the classes generated from XSD and turn it into an Element object that I can add to the ‘any’ List of the payload object?? For instance, if I have a DailyData class and I create and populate it with data:
DailyData dData = new DailyData();
dData.setID = 34;
dData.setValues = "3,5,76,23";
How do I add that ‘dData’ object to the ‘any’ List of the payload object? It has to be an Element. Do I do something with a JAXBContext marshaller? I’ve used that to dump the ‘dData’ object to the screen to check the XML structure.
I’m sure the answer is staring me in the face but I just can’t see it!
Dave
UPDATE: Got it working with the below code snippet:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().newDocument();
JAXBContext context = JAXBContext.newInstance(DailyData.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(dData, doc);
PayloadType payload = new PayloadType();
payload.getAny().add((Element)doc.getFirstChild());
The
List<Element>field type is usually generated by XJC when you have something like this in the schema:The key here is
processContents="skip", which means “anything goes” – any well-formed XML can go in here. Because it’s a free-for-all, all XJC can do is represent it as a DOM, and it becomes your responsibility to handle that payload.If you remove
processContents="skip", then JAXB will make an attempt to bind the payload to an object model, if it can match the payload XML to a class in theJAXBContext. In this case, XJC will generate this a field of typeList<Object>.This may not seem like a improvement, but this
Listcan containElement(if theJAXBContextdoesn’t recognise the payload as something it can bind to), orJAXBElement(if it does recognise it). The latter contains the bound version of the payload, and is much easier to handle.This is all described further here.
If you cannot modify the schema, and are stuck with
processContents="skip", then you’re going to have to jump through hoops. You can build anotherJAXBContextthat knows about your payload classes, and use that to marshal to anElement(using something likemarshaller.marshal(payload, new DOMResult()). You can then dump that element into the payload.