+1  A: 

This is how I would do it.

MessageFactory factory = MessageFactory.newInstance();           
SOAPMessage message = factory.createMessage();
SOAPBody body = message.getSOAPBody();
SOAPElement checkAccEl =  body
  .addChildElement("checkAccount", "to", "http://foo");

SOAPElement idEl = checkAccEl
  .addChildElement("id", "to", "http://foo");
idEl.addTextNode("test");

SOAPElement passwordEl = checkAccEl
  .addChildElement("password", "to", "http://foo");
passwordEl.addTextNode("test");

// print out the SOAP Message. How easy is this?!
ByteArrayOutputStream out = new ByteArrayOutputStream();
message.writeTo(out);
System.out.println(out.toString());

The first time you use the namespace 'to=http://foo' it is automatically declared on the element - checkAccount in this case. When you use the same namespace again, the XML won't need to declare it again, but will use the prefix.

The output looks like:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    <SOAP-ENV:Header/>
    <SOAP-ENV:Body>
        <to:checkAccount xmlns:to="http://foo"&gt;
            <to:id>test</to:id>
            <to:password>test</to:password>
         </to:checkAccount>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Which is what you want I think

Dunderklumpen
thank for your help Dunderklumpen, I changed my code so it looks like your since its easier to read. But looks like the issue isn't that and more like how i need to read and deal with the response. I updated my post so can you please look and see where i'm wrong? also i'm using eclipse , how do you know how exactly your output looks like?
techventure
I have added some code to show you how to print out the SOAP message. Of course this doesn't format the XML (it is all written on one line). You can use eclipse to format it by doing the following.1. copy the XML from the out console2. create a new text document in eclipse paste the contents3. save the document as <name>.xml4. Reopen the document and choose format (ctrl+shift+f).For small XML string this is not really worth the effort, but for large XML strings it can be very useful.
Dunderklumpen
If you want to see the SOAP request coming and going, you should consider using TCPMon. https://tcpmon.dev.java.net/ can act as a "man in the middle". You can configure TCPMon to listen for your SOAP Request and send it on to the proper server, it then passes the response back to you. Its an effective way of seeing what the request and the response look like.
Dunderklumpen
now i can see what the error is thanks to you:<pre><code><?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <soapenv:Fault> <faultcode xmlns:ns1="http://xml.apache.org/axis/">ns1:Client.NoSOAPAction</faultcode> <faultstring>no SOAPAction header!</faultstring> <detail> <ns2:hostname xmlns:ns2="http://xml.apache.org/axis/">foo1</ns2:hostname> </detail> </soapenv:Fault></code></pre>
techventure
does that mean i need to give it a header?
techventure
Yes, all soap requests need a SOAPAction header. From memory, it is something like: message.getMimeHeaders().addHeader("SOAPAction", <soap action>);. Check the web service WSDL for the action.
Dunderklumpen
dude i got it working :) thank you soo very much. all i needed was to add a header like you said. Just one last question, would the above code generally apply to javascript (Not syntax but how you work with body,headers..etc)? or is sending and receiving soap requests totally different?
techventure
Huzzah!!! I don't know javascript very well. I think Javascript has some built in stuff for calling web services, but I'm not really sure. Javascript is probably going to be quite different, but of course, the underlying principles will be the same. I am sure there are lots of people doing javascript and Web Services, so Googling will probably get you good results.
Dunderklumpen
Now I remeber, the key interface for Javascript is the XMLHttpRequest APItry: http://www.ibm.com/developerworks/webservices/library/ws-wsajax/ and http://articles.techrepublic.com.com/5100-10878_11-5887775.html (found after a super-quick google).
Dunderklumpen
thanks Dunderklumpen, you're great help, much appreciated.
techventure