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 Brian Reinhold <br...@lampreynetworks.com> on 2012/11/08 23:49:03 UTC

Axiom Class cast exception on iterator of that class!

Why does this code throw a java.lang.ClassCastException:
org.apache.axiom.om.impl.llom.OMTextImpl cannot be cast to
org.apache.axiom.om.OMElement

 

             MessageContext msgContext = data.getInMessageContext();

             SOAPHeader header = msgContext.getEnvelope().getHeader();

             Iterator<OMElement> iter = header.getDescendants(false);

             String user = "";

             while(iter.hasNext())

             {

                 OMElement element = iter.next();

                 if(element.getType() == OMNode.ELEMENT_NODE)

                 {

                     element = element.getFirstChildWithName(new
QName(wsseNamespace, "Username"));

                     if(element != null)

                     {

                         user = element.getText();

                     }

                 }

             }

 

The getDescendants() method of Axiom states that it returns an iterator over
OMElements. Yet I get that class cast exception with no way to guard against
it. Can someone explain how this can happen?

 

Thanks

 

Brian


Re: Axiom Class cast exception on iterator of that class!

Posted by Andreas Veithen <an...@gmail.com>.
On Sun, Nov 11, 2012 at 9:48 PM, James Grahn <gr...@gmail.com> wrote:
> On Sun, Nov 11, 2012 at 2:26 PM, Andreas Veithen
> <an...@gmail.com> wrote:
>> The Javadoc says this: "Get an iterator over all descendants of the
>> container. The items are returned in document order. Note that
>> attributes and namespace declarations are not considered descendants."
>> It doesn't restrict the result to elements.
>
> This kind of casting confusion could be resolved with the use of
> generics.   Any plans for updating the public APIs in that way in the
> future?

That is planned for Axiom 1.3:

http://ws.apache.org/axiom/roadmap.html

> James
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-dev-unsubscribe@axis.apache.org
> For additional commands, e-mail: java-dev-help@axis.apache.org
>

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


Re: Axiom Class cast exception on iterator of that class!

Posted by James Grahn <gr...@gmail.com>.
On Sun, Nov 11, 2012 at 2:26 PM, Andreas Veithen
<an...@gmail.com> wrote:
> The Javadoc says this: "Get an iterator over all descendants of the
> container. The items are returned in document order. Note that
> attributes and namespace declarations are not considered descendants."
> It doesn't restrict the result to elements.

This kind of casting confusion could be resolved with the use of
generics.   Any plans for updating the public APIs in that way in the
future?

James

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


Re: Axiom Class cast exception on iterator of that class!

Posted by Andreas Veithen <an...@gmail.com>.
On Thu, Nov 8, 2012 at 11:49 PM, Brian Reinhold
<br...@lampreynetworks.com> wrote:
> Why does this code throw a java.lang.ClassCastException:
> org.apache.axiom.om.impl.llom.OMTextImpl cannot be cast to
> org.apache.axiom.om.OMElement
>
>
>
>              MessageContext msgContext = data.getInMessageContext();
>
>              SOAPHeader header = msgContext.getEnvelope().getHeader();
>
>              Iterator<OMElement> iter = header.getDescendants(false);
>
>              String user = "";
>
>              while(iter.hasNext())
>
>              {
>
>                  OMElement element = iter.next();
>
>                  if(element.getType() == OMNode.ELEMENT_NODE)
>
>                  {
>
>                      element = element.getFirstChildWithName(new
> QName(wsseNamespace, "Username"));
>
>                      if(element != null)
>
>                      {
>
>                          user = element.getText();
>
>                      }
>
>                  }
>
>              }
>
>
>
> The getDescendants() method of Axiom states that it returns an iterator over
> OMElements. Yet I get that class cast exception with no way to guard against
> it. Can someone explain how this can happen?
>

The Javadoc says this: "Get an iterator over all descendants of the
container. The items are returned in document order. Note that
attributes and namespace declarations are not considered descendants."
It doesn't restrict the result to elements.

>
> Thanks
>
>
>
> Brian

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