Swing HTMLDocument 不包含所有 HTML 元素? [英] Swing HTMLDocument doesn't contain all the HTML elements?
问题描述
我有一个 Swing 应用程序,其中包含一个显示 HTML 文件的对话框.我是这样做的:
URL url = getClass().getResource("/resources/EULA.html");JDialog eulaDialog = new JDialog();JEditorPane eulaEP = new JEditorPane();尝试 {eulaEP.setPage(url);} catch (IOException e1) {//TODO 自动生成的 catch 块e1.printStackTrace();}JScrollPane scrollPane = new JScrollPane(eulaEP);eulaDialog.getContentPane().add(scrollPane);eulaDialog.setBounds(400, 200, 700, 600);eulaDialog.setVisible(true);
这符合我的预期.它正确显示 HTML 文件.
我现在尝试在文档中找到一个元素,所以我这样做:
HTMLDocument htm = (HTMLDocument) eulaEP.getDocument();元素 el = htm.getElement("unique_id");
但这会返回 null(即使该元素显示在对话框中).
我决定通过这样做来检查文档中保存了哪些元素:
for (ElementIterator iterator = new ElementIterator(htm); iterator.next() != null;)
然而这仅返回了 4 个元素;html、正文、p 和内容.我的 HTML 文件有很多(还有什么是内容?)我做错了什么?
澄清一下,HTML 包含一个按钮,我想向这个按钮添加一个 ActionListener,这样我就可以在我的 Java 代码中点击它.
在下面的例子中,
控制台:
BranchElement(div) 1,6元素:'BranchElement(html) 0,6',名称:'html',孩子:2,属性:1,叶:假属性:'名称',值:'html'元素:'BranchElement(head) 0,1',名称:'head',子元素:1,属性:1,叶子:false属性:'name',值:'head'元素:'BranchElement(p-implied) 0,1',名称:'p-implied',孩子:1,属性:1,叶:假属性:'name',值:'p-implied'元素:'LeafElement(content) 0,1',名称:'内容',孩子:0,属性:2,叶子:真属性:'CR',值:'真'属性:'名称',值:'内容'内容 (0-1): ''元素:'BranchElement(body) 1,6',名称:'body',子元素:1,属性:1,叶子:false属性:'name',值:'body'元素:'BranchElement(div) 1,6',名称:'div',子元素:1,属性:3,叶子:false属性:'对齐',值:'中心'属性:'id',值:'unique_id'属性:'名称',值:'div'元素:'BranchElement(p-implied) 1,6',名称:'p-implied',孩子:2,属性:1,叶:假属性:'name',值:'p-implied'元素:'LeafElement(content) 1,5',名称:'内容',孩子:0,属性:1,叶子:真属性:'名称',值:'内容'内容 (1-5):'测试'元素:'LeafElement(content) 5,6',名称:'内容',孩子:0,属性:2,叶子:真属性:'CR',值:'真'属性:'名称',值:'内容'内容 (5-6): ''
代码:
import java.awt.EventQueue;导入 java.util.Enumeration;导入 javax.swing.JEditorPane;导入 javax.swing.JFrame;导入 javax.swing.text.AttributeSet;导入 javax.swing.text.BadLocationException;导入 javax.swing.text.Element;导入 javax.swing.text.ElementIterator;导入 javax.swing.text.StyleConstants;导入 javax.swing.text.html.HTML;导入 javax.swing.text.html.HTMLDocument;/*** @see http://stackoverflow.com/a/5614370/230513*/公共类测试{私有静态最终字符串文本= ""+ "<头></头>"+ "<身体>"+ "<div align=center id=unique_id>测试</div>"+ "</body>"+ "</html>";public static void main(String[] args) 抛出异常 {EventQueue.invokeLater(new Test()::display);}私人无效显示(){JFrame f = new JFrame("Test");f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);JEditorPane jep = new JEditorPane("text/html", TEXT);jep.setEditable(false);f.add(jep);f.pack();f.setLocationRelativeTo(null);f.setVisible(true);HTMLDocument htmlDoc = (HTMLDocument) jep.getDocument();System.out.println(htmlDoc.getElement("unique_id"));ElementIterator iterator = new ElementIterator(htmlDoc);元素元素;while ((element = iterator.next()) != null) {尝试 {打印元素(htmlDoc,元素);} catch (BadLocationException e) {e.printStackTrace(System.err);}}}private void printElement(HTMLDocument htmlDoc, Element element) 抛出 BadLocationException {AttributeSet attrSet = element.getAttributes();System.out.println(""+ "元素:'" + element.toString().trim()+ "', 名称:'" + element.getName()+ "', children: " + element.getElementCount()+ ", 属性:" + attrSet.getAttributeCount()+ ", 叶子: " + element.isLeaf());枚举 attrNames = attrSet.getAttributeNames();而 (attrNames.hasMoreElements()) {对象属性 = attrNames.nextElement();System.out.println(" 属性:'" + attr + "',值:'"+ attrSet.getAttribute(attr) + "'");对象标签 = attrSet.getAttribute(StyleConstants.NameAttribute);if (attr == StyleConstants.NameAttribute&&标签 == HTML.Tag.CONTENT) {int startOffset = element.getStartOffset();int endOffset = element.getEndOffset();int length = endOffset - startOffset;System.out.printf(" 内容 (%d-%d): '%s'\n", startOffset,endOffset, htmlDoc.getText(startOffset, length).trim());}}}}
I have a swing application that contains a dialog displaying a HTML file. I do this like so:
URL url = getClass().getResource("/resources/EULA.html");
JDialog eulaDialog = new JDialog();
JEditorPane eulaEP = new JEditorPane();
try {
eulaEP.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JScrollPane scrollPane = new JScrollPane(eulaEP);
eulaDialog.getContentPane().add(scrollPane);
eulaDialog.setBounds(400, 200, 700, 600);
eulaDialog.setVisible(true);
This works as I expected. It displays the HTML file correctly.
I now try to find an element in the document so I do this:
HTMLDocument htm = (HTMLDocument) eulaEP.getDocument();
Element el = htm.getElement("unique_id");
But this returns null (even though the element is shown in the dialog).
I decided to check which elements were held in the document by doing this:
for (ElementIterator iterator = new ElementIterator(htm); iterator.next() != null;)
This however only returned 4 elements; html, body, p and content. My HTML file has a lot more (and what is content anyway?) What am I doing wrong?
Just to clarify, the HTML contains a button, I want to add an ActionListener to this button so I can catch a click on it in my java code.
In the example below, HTMLDocument::getElement(String id)
finds the Element
whose HTML.Attribute.id
attribute has the value "unique_id"
. The Element
is BranchElement(div) 1,6
I'm not sure where your Element
iteration goes awry, but you can see the unique_id
value in the BranchElement(div)
in the console output below. Because an HTMLDocument
models HTML, the enclosed HTMLReader
may synthesize HTML.Tag CONTENT
, such as the content in the implied paragraphs seen below.
Console:
BranchElement(div) 1,6
Element: 'BranchElement(html) 0,6', name: 'html', children: 2, attributes: 1, leaf: false
Attribute: 'name', Value: 'html'
Element: 'BranchElement(head) 0,1', name: 'head', children: 1, attributes: 1, leaf: false
Attribute: 'name', Value: 'head'
Element: 'BranchElement(p-implied) 0,1', name: 'p-implied', children: 1, attributes: 1, leaf: false
Attribute: 'name', Value: 'p-implied'
Element: 'LeafElement(content) 0,1', name: 'content', children: 0, attributes: 2, leaf: true
Attribute: 'CR', Value: 'true'
Attribute: 'name', Value: 'content'
Content (0-1): ''
Element: 'BranchElement(body) 1,6', name: 'body', children: 1, attributes: 1, leaf: false
Attribute: 'name', Value: 'body'
Element: 'BranchElement(div) 1,6', name: 'div', children: 1, attributes: 3, leaf: false
Attribute: 'align', Value: 'center'
Attribute: 'id', Value: 'unique_id'
Attribute: 'name', Value: 'div'
Element: 'BranchElement(p-implied) 1,6', name: 'p-implied', children: 2, attributes: 1, leaf: false
Attribute: 'name', Value: 'p-implied'
Element: 'LeafElement(content) 1,5', name: 'content', children: 0, attributes: 1, leaf: true
Attribute: 'name', Value: 'content'
Content (1-5): 'Test'
Element: 'LeafElement(content) 5,6', name: 'content', children: 0, attributes: 2, leaf: true
Attribute: 'CR', Value: 'true'
Attribute: 'name', Value: 'content'
Content (5-6): ''
Code:
import java.awt.EventQueue;
import java.util.Enumeration;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
/**
* @see http://stackoverflow.com/a/5614370/230513
*/
public class Test {
private static final String TEXT
= "<html>"
+ "<head></head>"
+ "<body>"
+ "<div align=center id=unique_id>Test</div>"
+ "</body>"
+ "</html>";
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Test()::display);
}
private void display() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JEditorPane jep = new JEditorPane("text/html", TEXT);
jep.setEditable(false);
f.add(jep);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
HTMLDocument htmlDoc = (HTMLDocument) jep.getDocument();
System.out.println(htmlDoc.getElement("unique_id"));
ElementIterator iterator = new ElementIterator(htmlDoc);
Element element;
while ((element = iterator.next()) != null) {
try {
printElement(htmlDoc, element);
} catch (BadLocationException e) {
e.printStackTrace(System.err);
}
}
}
private void printElement(HTMLDocument htmlDoc, Element element) throws BadLocationException {
AttributeSet attrSet = element.getAttributes();
System.out.println(""
+ "Element: '" + element.toString().trim()
+ "', name: '" + element.getName()
+ "', children: " + element.getElementCount()
+ ", attributes: " + attrSet.getAttributeCount()
+ ", leaf: " + element.isLeaf());
Enumeration attrNames = attrSet.getAttributeNames();
while (attrNames.hasMoreElements()) {
Object attr = attrNames.nextElement();
System.out.println(" Attribute: '" + attr + "', Value: '"
+ attrSet.getAttribute(attr) + "'");
Object tag = attrSet.getAttribute(StyleConstants.NameAttribute);
if (attr == StyleConstants.NameAttribute
&& tag == HTML.Tag.CONTENT) {
int startOffset = element.getStartOffset();
int endOffset = element.getEndOffset();
int length = endOffset - startOffset;
System.out.printf(" Content (%d-%d): '%s'\n", startOffset,
endOffset, htmlDoc.getText(startOffset, length).trim());
}
}
}
}
这篇关于Swing HTMLDocument 不包含所有 HTML 元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!