Creating SOAP with Axis2

Hi,

i am using axis2 version .95. I am trying to build a SOAP client. I have the following code from the Axis website:


import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.ws.commons.om.OMElement;

import org.apache.ws.commons.om.OMAbstractFactory;
import org.apache.ws.commons.om.OMFactory;
import org.apache.ws.commons.om.OMNamespace;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import java.io.StringWriter;


public class SOAPTest {
    private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyService");
      /**
       * @param args
       */
      public static void main(String[] args) {
        try {
            OMElement payload = getEchoOMElement();
            Options options = new Options();
            options.setTo(targetEPR);
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);

            //Blocking invocation
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);
            OMElement result = sender.sendReceive(payload);

            StringWriter writer = new StringWriter();
            result.serialize(XMLOutputFactory.newInstance()
                    .createXMLStreamWriter(writer));
            writer.flush();

            System.out.println(writer.toString());

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        }
      }
      
    public static OMElement getEchoOMElement() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://example1.org/example1", "example1");
        OMElement method = fac.createOMElement("echo", omNs);
        OMElement value = fac.createOMElement("Text", omNs);
        value.addChild(fac.createText(value, "Axis2 Echo String "));
        method.addChild(value);
       
        return method;
    }


All of the examples i have seen are similar to this, however i need something different. I already have the SOAP body as an XML file out on the file system. I would like to read in this file rather than creating the nodes, etc as in the example above. Is there a way to do this? please provide sample code.

Thanks,
Reza
da4thrzaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
If you already have it in a file, you can read it directly:

http://www.javaalmanac.com/egs/java.io/ReadLinesFromFile.html
0
da4thrzaAuthor Commented:
i know how to read a text file. that is not my problem. What i have in a file is the soap body, not the whole envelope. i need to  know how to bring that into the rest of the envelope
0
Mayank SAssociate Director - Product EngineeringCommented:
In your existing code, whereever you create the soap body (inside the envelope), you need to replace that part by the String that you read from the file.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

da4thrzaAuthor Commented:
if you look at the code sample above, the body is being generated as such:
    public static OMElement getEchoOMElement() {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace(
                "http://example1.org/example1", "example1");
        OMElement method = fac.createOMElement("echo", omNs);
        OMElement value = fac.createOMElement("Text", omNs);
        value.addChild(fac.createText(value, "Axis2 Echo String "));
        method.addChild(value);
       
        return method;
    }

this... each node is being created individually. i want to know how to stick an xml file in there insetead
0
Mayank SAssociate Director - Product EngineeringCommented:
I haven't worked with that API, but basically it looks like:

>> value.addChild(fac.createText(value, "Axis2 Echo String "));

will have to be replaced with value.addChild ( fac.createText ( value, "THE TEXT READ FROM THE FILE" ) ) ;

where THE TEXT READ FROM THE FILE is what you read from the file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
da4thrzaAuthor Commented:
thanks... ill give that a shot!
0
Mayank SAssociate Director - Product EngineeringCommented:
Provided what you have in the file is exactly what is supposed to be the replacement for "Axis2 Echo String "
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.