[jdom-interest] unexpected getText() results

David Kavanagh dak at dotech.com
Mon Jul 22 11:09:28 PDT 2002


You are correct, of course! I couldn't see it until you pointed out that 
it must be there!

Thanks!

Geoff Rimmer wrote:

>David Kavanagh <dak at dotech.com> writes:
>
>><Document>
>><String>this is a line of text<br/>and another line of text</String>
>></Document>
>>
>>I call getContent() on the <String> element. I would expect to get a
>>List containing (Text, Element, Text) objects.
>>
>
>Correct.
>
>>[...] Then, I call getText() on the last Text object. I would expect
>>to get "and another line of text", but I don't. I get "this is a
>>line of textand another line of text".
>>
>
>Sounds like a bug in your code.
>
>The following code:
>
>    import java.io.StringReader;
>    import java.util.Iterator;
>    import java.util.List;
>    
>    import org.jdom.Element;
>    import org.jdom.Text;
>    import org.jdom.input.SAXBuilder;
>    
>    public class TextXml
>    {
>        public static void main( String[] args )
>        {
>            String s = "<Document><String>this is a line of text"
>                + "<br/>and another line of text</String></Document>";
>    
>            try
>            {
>                List list = new SAXBuilder().build( new StringReader( s ) )
>                    .getRootElement().getChild( "String" ).getContent();
>                
>                for ( Iterator iter = list.iterator(); iter.hasNext(); )
>                {
>                    Object obj = iter.next();
>                    if ( obj instanceof Text )
>                    {
>                        Text text = (Text)obj;
>                        System.out.println( "Text: " + text.getText() );
>                    }
>                    else if ( obj instanceof Element )
>                    {
>                        Element element = (Element)obj;
>                        System.out.println( "Element: " + element.getName() );
>                    }
>                    else 
>                    {
>                        System.out.println( "Something else" );
>                    }
>                }
>            }
>            catch ( Exception cause )
>            {
>                System.err.println( "Exception: " + cause );
>            }
>        }
>    }
>    
>outputs the following:
>
>    Text: this is a line of text
>    Element: br
>    Text: and another line of text
>
>as expected, so it doesn't look like there's a problem with JDOM.
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20020722/38424c0c/attachment.htm


More information about the jdom-interest mailing list