当我运行以下代码时:
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);
}
我得到以下输出:
LeafElement(dyn) 1,2
LeafElement(content) 2,3
LeafElement(dyn) 3,4
LeafElement(content) 4,5
但是,如果我将值更改content
为" "
:
String content = " ";
我得到这个输出:
LeafElement(dyn) 1,2
LeafElement(dyn) 2,3
LeafElement(content) 3,4
为什么内容是为LeafElement
构造的"x"
,而不是为构造的" "
?我想LeafElement
为" "
. 我做错了什么还是 or 有HTMLDocument
问题HTMLEditorKit
?