问题描述
您好,
我希望这里有人知道并使用Dom4j库进行Java。我明天有一个任务,但几乎没有任何成就。所以,我需要你的帮助。
使用Dom4j库,我被要求解析一个HTML文档,然后为它的DOM树创建一个.dot文件。 GraphViz应用程序将使用DOT语言的此文件。在这里,我只考虑文件的文本节点。
使用库的内置函数解析HTML文档是可以的。我可以在Eclipse的调试模式下看到文本节点的父节点。但我不能通过在PrintWriter类型中定义的输出流以DOT语言的语法在.dot文件上写这些数据。
首先,在块中public void visit(文本文本)方法,我试图通过steam创建一个新的.dot文件。然后,我尝试在文件上写digraph G {\ n作为文档的开头。然后,我将以下行构造为(父节点) - >(子节点);的语法DOM树中每个父子对的DOT语言。
writer.println(text.getParent()。getStringValue()+ - >+ text.getStringValue()+;);
但是这行首先将整个文档写在.dot文件上,然后放一个 - >和;正如我在代码中写的那样。
我希望你能解决这个问题。我能在这做什么?提前谢谢。
Hello,
I hope here is somebody who knows and uses Dom4j library for Java. I have an assignment for tomorrow, but have achieved almost nothing. So, I need your help.
Using Dom4j library, I am asked to parse an HTML document, then create a .dot file for its DOM tree. This file in DOT language will be used by GraphViz application. Here, I'll consider just text nodes of the file.
Parsing an HTML document is okay using the library's built-in functions. I can see a text node's parent in the debugging mode of Eclipse. But I cannot write these data over a .dot file in the syntax of DOT language via an output steam defined in the type of PrintWriter.
First, in the block of public void visit(Text text) method, I tried to create a new .dot file via the steam. Then, I tried to write "digraph G{\n" over the file as a beginning for the document. Then, I put the following line to construct the syntax of "(parent node) -> (child node);" in DOT language for each parent-child pair in the DOM tree.
writer.println(text.getParent().getStringValue() + "->" + text.getStringValue() + ";");
But this line first writes the entire document over the .dot file, then puts a "->" and a ";" as I've written in the code.
I hope you get the issue. What can I do here? Thank you in advance.
推荐答案
这篇关于如何使用Dom4j库为Java创建HTML文件的DOM树的.dot文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!