You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by "Yu, Joel, Contractor" <Jo...@asamra.hoffman.army.mil> on 2004/06/22 20:27:53 UTC

what's wrong with this code

when i run the following code, i get:
soapelement is:<headerChild xmlns="">test</headerChild> 
header is:<ns1:header xmlns:ns1="http://localhost/"></ns1:header>

why doesn't the header include soapelement?


import org.apache.axis.message.SOAPHeaderElement;
import org.apache.axis.message.SOAPEnvelope;
import javax.xml.soap.SOAPElement;
import org.apache.axis.utils.XMLUtils;

public class Test {
  public Test() {
  }


  public static void main(String[] args) {
    try {
      SOAPEnvelope soapEnvelope = new SOAPEnvelope();
      SOAPHeaderElement soapHeader =
          new SOAPHeaderElement(
          XMLUtils.StringToElement(
          "http://localhost/", "header",
          ""));
      SOAPElement se = soapHeader.addChildElement("headerChild", "", "");
      se.addTextNode("test");
      soapEnvelope.addHeader(soapHeader);
System.out.println("soapelement is:"+se);
System.out.println("header is:"+soapHeader);
    }
    catch(Exception e) {
      e.printStackTrace();
    }

  }
}

RE: what's wrong with this code

Posted by Jarek Gawor <ga...@mcs.anl.gov>.
It depends which constructor you use like I mentioned before. In this case,
to the "ReplyTo" header you can add children and they will be serialized
fine. But for the "Address" element, you could still add children to it but
they would not be serialized (see MessageElement.outputImpl() for details). 
In a better world, addChild() and related operations should thrown an
exception if we know that they won't be serialized in the first place.
MessageElement class still needs a bit of work.

Jarek

> -----Original Message-----
> From: Sanjiva Weerawarana [mailto:sanjiva@opensource.lk] 
> Sent: Tuesday, June 22, 2004 11:22 PM
> To: axis-dev@ws.apache.org
> Subject: Re: what's wrong with this code
> 
> 
> Actually you can add children .. I did the following 
> yesterday to generate some WS-Addressing headers and it worked fine:
> 
>     SOAPHeaderElement replyToHeader =
>       new 
> SOAPHeaderElement("http://schemas.xmlsoap.org/ws/2003/03/addressing",
>                             "ReplyTo");
>     replyToHeader.addChild
>       (new MessageElement 
> ("http://schemas.xmlsoap.org/ws/2003/03/addressing",
>                            "Address",
> 
> (Object)"http://schemas.xmlsoap.org/ws/2003/03/addressing/role
> /anonymous"));
> 
> Sanjiva.
> 
> ----- Original Message ----- 
> From: "Jarek Gawor" <ga...@mcs.anl.gov>
> To: <ax...@ws.apache.org>
> Sent: Wednesday, June 23, 2004 3:08 AM
> Subject: RE: what's wrong with this code
> 
> 
> Once you create some subclass of MessageElement (e.g. 
> SOAPHeaderElement) with a DOM Element, or a some value you 
> cannot add children to it. Well, you can but they won't be 
> serialized properly as your test program shows. In this case, 
> just use a different constructor - for example:
> 
>     SOAPHeaderElement soapHeader =
>           new SOAPHeaderElement("http://localhost/", "header");
> 
> This hopefully be improved one way.
> 
> Jarek
> 
> > -----Original Message-----
> > From: Yu, Joel, Contractor 
> > [mailto:Joel.Yu.Ctr@asamra.hoffman.army.mil]
> > Sent: Tuesday, June 22, 2004 2:28 PM
> > To: 'axis-dev@ws.apache.org'
> > Subject: what's wrong with this code
> >
> >
> > when i run the following code, i get:
> > soapelement is:<headerChild xmlns="">test</headerChild> header 
> > is:<ns1:header xmlns:ns1="http://localhost/"></ns1:header>
> >
> > why doesn't the header include soapelement?
> >
> >
> > import org.apache.axis.message.SOAPHeaderElement;
> > import org.apache.axis.message.SOAPEnvelope;
> > import javax.xml.soap.SOAPElement;
> > import org.apache.axis.utils.XMLUtils;
> >
> > public class Test {
> >   public Test() {
> >   }
> >
> >
> >   public static void main(String[] args) {
> >     try {
> >       SOAPEnvelope soapEnvelope = new SOAPEnvelope();
> >       SOAPHeaderElement soapHeader =
> >           new SOAPHeaderElement(
> >           XMLUtils.StringToElement(
> >           "http://localhost/", "header",
> >           ""));
> >       SOAPElement se = 
> soapHeader.addChildElement("headerChild", "", 
> > "");
> >       se.addTextNode("test");
> >       soapEnvelope.addHeader(soapHeader);
> > System.out.println("soapelement is:"+se); 
> System.out.println("header 
> > is:"+soapHeader);
> >     }
> >     catch(Exception e) {
> >       e.printStackTrace();
> >     }
> >
> >   }
> > }
> >
> >
> 
> 
> 


Re: what's wrong with this code

Posted by Sanjiva Weerawarana <sa...@opensource.lk>.
Actually you can add children .. I did the following yesterday to generate
some WS-Addressing headers and it worked fine:

    SOAPHeaderElement replyToHeader =
      new
SOAPHeaderElement("http://schemas.xmlsoap.org/ws/2003/03/addressing",
                            "ReplyTo");
    replyToHeader.addChild
      (new MessageElement
("http://schemas.xmlsoap.org/ws/2003/03/addressing",
                           "Address",

(Object)"http://schemas.xmlsoap.org/ws/2003/03/addressing/role/anonymous"));

Sanjiva.

----- Original Message ----- 
From: "Jarek Gawor" <ga...@mcs.anl.gov>
To: <ax...@ws.apache.org>
Sent: Wednesday, June 23, 2004 3:08 AM
Subject: RE: what's wrong with this code


Once you create some subclass of MessageElement (e.g. SOAPHeaderElement)
with a DOM Element, or a some value you cannot add children to it. Well, you
can but they won't be serialized properly as your test program shows. In
this case, just use a different constructor - for example:

    SOAPHeaderElement soapHeader =
          new SOAPHeaderElement("http://localhost/", "header");

This hopefully be improved one way.

Jarek

> -----Original Message-----
> From: Yu, Joel, Contractor
> [mailto:Joel.Yu.Ctr@asamra.hoffman.army.mil]
> Sent: Tuesday, June 22, 2004 2:28 PM
> To: 'axis-dev@ws.apache.org'
> Subject: what's wrong with this code
>
>
> when i run the following code, i get:
> soapelement is:<headerChild xmlns="">test</headerChild>
> header is:<ns1:header xmlns:ns1="http://localhost/"></ns1:header>
>
> why doesn't the header include soapelement?
>
>
> import org.apache.axis.message.SOAPHeaderElement;
> import org.apache.axis.message.SOAPEnvelope;
> import javax.xml.soap.SOAPElement;
> import org.apache.axis.utils.XMLUtils;
>
> public class Test {
>   public Test() {
>   }
>
>
>   public static void main(String[] args) {
>     try {
>       SOAPEnvelope soapEnvelope = new SOAPEnvelope();
>       SOAPHeaderElement soapHeader =
>           new SOAPHeaderElement(
>           XMLUtils.StringToElement(
>           "http://localhost/", "header",
>           ""));
>       SOAPElement se =
> soapHeader.addChildElement("headerChild", "", "");
>       se.addTextNode("test");
>       soapEnvelope.addHeader(soapHeader);
> System.out.println("soapelement is:"+se);
> System.out.println("header is:"+soapHeader);
>     }
>     catch(Exception e) {
>       e.printStackTrace();
>     }
>
>   }
> }
>
>



RE: what's wrong with this code

Posted by Jarek Gawor <ga...@mcs.anl.gov>.
Once you create some subclass of MessageElement (e.g. SOAPHeaderElement)
with a DOM Element, or a some value you cannot add children to it. Well, you
can but they won't be serialized properly as your test program shows. In
this case, just use a different constructor - for example:

    SOAPHeaderElement soapHeader =
          new SOAPHeaderElement("http://localhost/", "header");

This hopefully be improved one way.

Jarek

> -----Original Message-----
> From: Yu, Joel, Contractor 
> [mailto:Joel.Yu.Ctr@asamra.hoffman.army.mil] 
> Sent: Tuesday, June 22, 2004 2:28 PM
> To: 'axis-dev@ws.apache.org'
> Subject: what's wrong with this code
> 
> 
> when i run the following code, i get:
> soapelement is:<headerChild xmlns="">test</headerChild> 
> header is:<ns1:header xmlns:ns1="http://localhost/"></ns1:header>
> 
> why doesn't the header include soapelement?
> 
> 
> import org.apache.axis.message.SOAPHeaderElement;
> import org.apache.axis.message.SOAPEnvelope;
> import javax.xml.soap.SOAPElement;
> import org.apache.axis.utils.XMLUtils;
> 
> public class Test {
>   public Test() {
>   }
> 
> 
>   public static void main(String[] args) {
>     try {
>       SOAPEnvelope soapEnvelope = new SOAPEnvelope();
>       SOAPHeaderElement soapHeader =
>           new SOAPHeaderElement(
>           XMLUtils.StringToElement(
>           "http://localhost/", "header",
>           ""));
>       SOAPElement se = 
> soapHeader.addChildElement("headerChild", "", "");
>       se.addTextNode("test");
>       soapEnvelope.addHeader(soapHeader);
> System.out.println("soapelement is:"+se); 
> System.out.println("header is:"+soapHeader);
>     }
>     catch(Exception e) {
>       e.printStackTrace();
>     }
> 
>   }
> }
> 
>