0

I'm trying to follow this post.

To reproduce a sample xml request I have in hand. The problem is that this particular request is pretty long with very deep structure, and I just get tired of adding child elements and keeping track of the structure with my eyes.

Below is some sample code that summarised what I have been doing. The request I am reproducing has at least 5 layers and more than 50 elements. It's supposed to be an application form which contains candidates personal information.

// SOAP Envelop...
// SOAP Body
        SOAPBody soapBody = envelope.getBody();
        SOAPElement aaa = soapBody.addChildElement("aaaName", "", "http://my.uri.aaa");
        SOAPElement bbb = aaa.addChildElement("bbbName", "", "http://my.uri.bbb");
        SOAPElement ccc = bbb.addChildElement("cccName");
        SOAPElement ddd = ccc.addChildElement("dddName");

        //... and so on ...        

        SOAPElement dddChild1 = ddd.addChildElement("dddChild1Name");
        dddChild1.addTextNode("I'm dddChild1");
        SOAPElement dddChild2 = ddd.addChildElement("dddChild2Name");
        SOAPElement dddGrandChild2 = dddChild2.addChildElement("dddGrandChild2Name");
        dddGrandChild2.addTextNode("I'm dddGrandChild2");

        //... and so on ...

Could anyone give some advice on how to make it a bit easier to construct/read/manage?

TempieD
  • 1
  • 1

1 Answers1

0

If your request is that large, I would recommend creating Java objects from the WSDL or XSD and then using JAXB to marshal the Java objects to the XML.

Mkyong has a simple JAXB example: https://www.mkyong.com/java/jaxb-hello-world-example/

Note: You're going to have to instantiate and populate the objects, but I would think that would be easier to manage than the way you're doing it now.

If you're using Eclipse, you can generate the objects using the steps found in this answer: Generate web service java class from WSDL in eclipse

Jeremy Smith
  • 311
  • 2
  • 13