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