Previous section   Next section
Text javax.xml.soap

SAAJ 1.1; JWSDP 1.0, J2EE 1.4
public interface Text extends Node {
// Public Instance Methods
    public abstract boolean isComment(  ); 
}

Text is a subinterface of Node that holds the text parts of a SOAP message. A Text node can only be created by using the addTextNode( ) method of SOAPElement, as shown in the following code extract:

SOAPElement element = body.addChildElement("BookTitle");
SOAPElement element2 = element.addTextNode("J2ME in a Nutshell");

This code creates an element called BookTitle and adds the given text as its value, resulting in the following XML when the message is serialized:

    <BookTitle>J2ME in a Nutshell</BookTitle>

The value returned by the addTextNode( ) method is actually a reference to the SOAPElement beneath which the Text node was added. In the code extract shown previously, therefore, element2 is set to the same value as element. The value of the text associated with a SOAPElement can be obtained by calling getValue( ) on the element itself, rather than by first obtaining a reference to the intervening Text object:

// Returns "J2ME in a Nutshell"
String text = element.getValue

In fact, there is no direct way to get a reference to the Text element itself. The only way to do this is to use the getChildElements( ) method of the parent SOAPElement and search for a child of type Text:

Iterator iter = element.getChildElements;
while (iter.hasNext() {
       Node node = (Node)iter.next();
       if (node instanceof Text) {
           // This also prints "J2ME in a Nutshell"
           System.out.println("Value of Text node " + node.getValue());
           break;
       }
}

In theory, a Text node may also represent an XML comment, although it is unlikely that a SOAP message would contain one. The isComment( ) method can be used to detect comments.


  Previous section   Next section