views:

273

answers:

1

When I run the following code:

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import javax.swing.text.BadLocationException;
import javax.swing.text.EditorKit;
import javax.swing.text.Element;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
    .
    .
    .
     String content = "x";
     String html = "<html><body><dyn/>" + content + "<dyn/></body></html>";
     final Reader reader = new StringReader(html);
     final EditorKit editorKit = new HTMLEditorKit();

     HTMLDocument hTMLDocument = new HTMLDocument();
     editorKit.read(reader, hTMLDocument, 0);
     Element defaultRootElement = hTMLDocument.getDefaultRootElement();
     Element branchElement = defaultRootElement.getElement(1).getElement(0);
     for (int i = 0; i < branchElement.getElementCount(); i++) {
      Element element = branchElement.getElement(i);
      System.out.print(element);
     }

I get the following output:

LeafElement(dyn) 1,2
LeafElement(content) 2,3
LeafElement(dyn) 3,4
LeafElement(content) 4,5

However, if I change the value of content to " ":

 String content = " ";

I get this output:

LeafElement(dyn) 1,2
LeafElement(dyn) 2,3
LeafElement(content) 3,4

Why is a content LeafElement constructed for "x", but not for " "? I want a LeafElement to be constructed for " ". Am I doing something wrong or is this a problem with HTMLDocument or HTMLEditorKit?

A: 

Don't know much about the editor kit buy maybe you can use "& n b s p ;" (without the spaces) instead of " ".

camickr
I've thought about that. The HTML is coming from elsewhere (and is much more complex than what I'm showing). We would have to insert the " " characters, which is certainly possible, if it comes to that. But I'm hoping for an explanation of why this is happening.
Paul Reiners