You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by Gabriel Moura <ga...@frb.br> on 2006/05/22 21:58:54 UTC

show a method return in xml response

hi, i am brazilian student and speak english very well. :D
my doubt is:

i want show only "xxxReturn" in the aplication..
my client is generated by wsdl2..  
i obtain xml response using:
-------------------------------------------
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
_call.getMessageContext().getMessage().writeTo(baos);
String XMLContents = new java.lang.String(baos.toByteArray(), "UTF-8");
System.out.println(XMLContents);
-----------------------------------------------

How i show only "xxxReturn" for example?
anyone can help-me?

--
View this message in context: http://www.nabble.com/show+a+method+return+in+xml+response-t1664824.html#a4511532
Sent from the Axis - User forum at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-user-help@ws.apache.org


Re: show a method return in xml response

Posted by robert lazarski <ro...@gmail.com>.
Here's an example of what I mean:

package cafe;

import static java.lang.System.out;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


/** Um exemplo como a cliente pode pegar um nó . */
public class GetSoapNode {

    /** Run the client.
     * @param args for main() */
    public static void main(String[] args) {
        try {
            doGetNode();
            out.println("doGetNode completed!!!");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** Executa a chamada. */
    private static void doGetNode() {

        String xml = "<?xml version='1.0'
encoding='utf-8'?><soapenv:Envelope xmlns:soapenv=\"
http://schemas.xmlsoap.org/soap/envelope/\"><soapenv:Header
/><soapenv:Body><simpleLoginResponse
xmlns=\"http://simpleNS/types\"><soap_session_id>my
random string</soap_session_id><web_user_name xmlns:xsi=\"
http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"
/></simpleLoginResponse></soapenv:Body></soapenv:Envelope>";

        try {
            Document doc = getDocument(xml);
            NodeList nodeList = doc.getElementsByTagName("soap_session_id");

            // What you do next depends a lot on what is in myNode -
            // you may have attributes, child elements, etc.
            Node myNode = nodeList.item(0);
            out.println(getNodeToString(myNode));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** Converter node para string
    @param   nodeName  node a converter
    @return  String    String convertida
    @throws  XMLHelperException
    */
    private static final String getNodeToString(Node node) throws Exception
{
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", "yes");

        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        DOMSource source = new DOMSource( node );
        transformer.transform( source, result );

        return sw.getBuffer().toString();
    }

    /** Convert String to a W3C XML Document.
    @param xmlString String to convert
    @return Document W3C XML Document
    @throws XMLHelperException all errors
    */
    private static final Document getDocument(String xmlString)
        throws Exception {

        DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setIgnoringElementContentWhitespace(false);
        DocumentBuilder builder = factory.newDocumentBuilder();

        InputSource isXml = new InputSource(new StringReader(xmlString));
        return builder.parse(isXml);
    }
}

You can change the argument in doc.getElementsByTagName("soap_session_id") -
to any other node to get an idea of what the code is doing.

BTW, since you are studying I recommend using Axis2 - its the future ;-) .
Here's a tutorial in português:

http://www.braziloutsource.com/wss2.html

HTH,
Robert
http://www.braziloutsource.com/


On 5/22/06, Gabriel Moura <ga...@frb.br> wrote:
>
>
> well,
>
> I am beginning and I did not understand very well.
> you can detail as you show the result? for example,
> System.out.println(????????).
>
> I want to show in my java aplication for example, only:
> <helloReturn xsi:type="xsd:string">HELLO TESTING....</helloReturn>
>
> thanks!
> --
> View this message in context:
> http://www.nabble.com/show+a+method+return+in+xml+response-t1664824.html#a4513290
> Sent from the Axis - User forum at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
> For additional commands, e-mail: axis-user-help@ws.apache.org
>
>

Re: show a method return in xml response

Posted by Gabriel Moura <ga...@frb.br>.
well,

I am beginning and I did not understand very well.
you can detail as you show the result? for example,
System.out.println(????????).

I want to show in my java aplication for example, only:   
<helloReturn xsi:type="xsd:string">HELLO TESTING....</helloReturn>

thanks!
--
View this message in context: http://www.nabble.com/show+a+method+return+in+xml+response-t1664824.html#a4513290
Sent from the Axis - User forum at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-user-help@ws.apache.org


Re: show a method return in xml response

Posted by robert lazarski <ro...@gmail.com>.
Assuming you have a 'xxxReturn' as a node in your soap message - take a look
at tcpmon or the soap applet, etc, if you need to...

I think what you want is to get a node and show just that - I think
axis 1.xallows you to get the Document from Message Context directly,
but anyways:

           DocumentBuilderFactory factory =
                DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setIgnoringElementContentWhitespace(false);
            DocumentBuilder builder = factory.newDocumentBuilder();

            InputSource isXml = new InputSource(new
StringReader(XMLContents));
            Document doc = builder.parse(isXml);
            NodeList nodeList = doc.getElementsByTagName("xxxReturn");

            // What you do next depends a lot on what is in myNode -
            // you may have attributes, child elements, etc.
            Node myNode = nodeList.item(0);

If that isn't what you want, try and rephrase the question.

HTH,
Robert
http://www.braziloutsource.com/

On 5/22/06, Gabriel Moura <ga...@frb.br> wrote:
>
>
> hi, i am brazilian student and speak english very well. :D
> my doubt is:
>
> i want show only "xxxReturn" in the aplication..
> my client is generated by wsdl2..
> i obtain xml response using:
> -------------------------------------------
> java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
> _call.getMessageContext().getMessage().writeTo(baos);
> String XMLContents = new java.lang.String(baos.toByteArray(), "UTF-8");
> System.out.println(XMLContents);
> -----------------------------------------------
>
> How i show only "xxxReturn" for example?
> anyone can help-me?
>
> --
> View this message in context:
> http://www.nabble.com/show+a+method+return+in+xml+response-t1664824.html#a4511532
> Sent from the Axis - User forum at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: axis-user-unsubscribe@ws.apache.org
> For additional commands, e-mail: axis-user-help@ws.apache.org
>
>