views:

178

answers:

2

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());
A: 

First thing, any type of element can contain any XML inside it. So Schema validation should work fine even if you add DailyData class XML representation in this.

Now for your other question about marshaling DailyData class. If this class was generated using xjc compiler itself, this class (or some other generated class) would already know the way to convert this object into XML.

I have not used this directly, but the following link should help you

http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/rwbs_xjc.html

Fazal
+1  A: 

The List<Element> field type is usually generated by XJC when you have something like this in the schema:

<xs:any processContents="skip" maxOccurs="unbounded" minOccurs="0" />

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 the JAXBContext. In this case, XJC will generate this a field of type List<Object>.

This may not seem like a improvement, but this List can contain Element (if the JAXBContext doesn't recognise the payload as something it can bind to), or JAXBElement (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 another JAXBContext that knows about your payload classes, and use that to marshal to an Element (using something like marshaller.marshal(payload, new DOMResult()). You can then dump that element into the payload.

skaffman
Thanks for the explanation and I'll check out the link. Since the XSD files are provided by a third-party, I don't want to change them. So, hoop-jumping it is! The way I've done it involves marshalling similar to your suggestion. My code runs without errors; the test will be to see if 'their' webservice accepts what I send, once test data is available in their system...
DaveKub
Got it working as suggested! I've put my code at the bottom of my original question.
DaveKub