[jdom-interest] unexpected getText() results

Geoff Rimmer geoff.rimmer at sillyfish.com
Mon Jul 22 10:05:12 PDT 2002


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.

-- 
Geoff Rimmer <> geoff.rimmer at sillyfish.com <> www.sillyfish.com
www.sillyfish.com/phone - Make savings on your BT and Telewest phone calls
UPDATED 07/06/2002: 521 destinations, 14 schemes (incl. 10p/min to mobiles)



More information about the jdom-interest mailing list