2015-07-01 177 views
0

我试图把关闭标签的关闭自动生成和我提到了这个链接关闭自动关闭标签在jsoup

How to turn off automatic generation of close tags </tagName> in Jsoup?

String html="<A HREF=\"#Item1\">\n" 
       + "<p style=\"font-family:times;margin-top:12pt;margin-left:0pt;\">\n" 
       + "<FONT SIZE=2>Item&nbsp;1.</FONT>\n" 
       + "</A>"; 
     Document document = Jsoup.parse(html,"",Parser.xmlParser()); 

但是当我尝试,我没有得到任何输出和我认为它会进入一个不确定的循环或其他东西。

这是我正努力代码:(没有输出和挂)

String html = "<table>" 
       + "<tr align='top'>" 
       + "<th><font>Link</th>" 
       + "</tr>" 
       + "</table>"; 

     Document document = Jsoup.parse(html,"",Parser.xmlParser()); 
     System.out.println(document.toString()); 

谁能告诉我是什么错误? 我需要的是某种输出说终端标签丢失。


编辑 - 对不起有一些问题,我eclipse.Anyway现在没有无限循环,但我的输出如下

String html = "<table>" 
       + "<tr align='top'>" 
       + "<th><font>Link</th>" 
       + "</tr>" 
       + "</table>"; 

     Document document = Jsoup.parse(html,"",Parser.xmlParser()); 

      System.out.println("UNPARSED = \n"+html + "\n---------------"); 
     System.out.println("parsed:" + document.toString()); 

输出

UNPARSED = 
<table><tr align='top'><th><font>Link</th></tr></table> 
--------------- 
parsed:<table> 
<tr align="top"> 
    <th><font>Link</font></th> 
</tr> 
</table> 

我不想让</font>待加入。


编辑 -

我通过Jsoup解析之前,使用正则表达式检查固定它。

回答

0

@Abi我不认为例子可以删除close tag,即使您使用xmlParser解析您的HTML,Jsoup仍然会增加close tag到未关闭标签。因为对于xmlhtml节点必须具有封闭标签的开放标签。你的例子证明了这一点。

我认为你可以使用regexp来做到这一点。

+0

我想在所有开始和结束标记之间做一个匹配,看看它是否正确匹配。 – Abi

+0

你能举个例子吗? – chengpohi

+0

在我的示例中,没有结束标记。我希望这一点被指出。 – Abi