JTidy HTML2XML

JTidy 一个能将HTML格式转换成XML的工具。


public class TestJTidy
{
public static void main(String[] args)
{
Tidy tidy = new Tidy(); // obtain a new Tidy instance
tidy.setXmlOut(true); // set desired config options using tidy setters
tidy.setQuoteNbsp(false);
tidy.setQuoteMarks(false);
tidy.setQuoteAmpersand(false);
tidy.setCharEncoding(Configuration.RAW);
try
{
FileInputStream in = new FileInputStream(new File("c://a.html"));
FileOutputStream out = new FileOutputStream(new File("output.xml"));
tidy.parseDOM(in, out );
}
catch (IOException e)
{
e.printStackTrace();
}
}
}


这里是从文件输入处理输出一个文件。 当然你可以不输出文件,parseDOM方法也是调用的parse方法。放回一个Document对象。你可以针对Document来进行业务处理。

[b]JTidy 与NekoHTML[/b]


对这两者进行比较,似乎勉为其难。不过,我看NekoHTML时,发现JTidy也有解析HTML的功能,JTidy最新的班本是2001年8月发布的,一看日期就让我失望,不过呢,好像在2009年7月31日发布了新版本。JTidy主要是整理(tidy)HTML文件的,更多的是关注HTML的规范,尤其是当我看到那一堆设置参数时,不由得喜欢起来。Jtidy好用的原因是因为它定制功能很强大 ,Tidy类里面有一堆setter/getter函数,就是用来对功能进行定制的。 要想整理 HTML,用JTidy没错。 附表:HTML Tidy设置参考列表。

[quote]
public void setIndentContent(boolean IndentContent) 是否使用缩进
public void setSmartIndent(boolean SmartIndent) 节点结束后,是否另起一行
public void setQuoteMarks(boolean QuoteMarks) 用 "替换 "
public void setQuoteNbsp(boolean QuoteNbsp)
public Document parseDOM(java.io.InputStream in,java.io.OutputStream out) 转换为DOM对象
etAltText(java.lang.String altText)
加上默认的alt属性值
setBreakBeforeBR(boolean breakBeforeBR)
在换行<br />之前加一空行
setCharEncoding(int charencoding)
已废弃
setConfigurationFromFile(java.lang.String filename)
从文件中读取配置信息
setConfigurationFromProps(java.util.Properties props)
从properties中读取配置信息
setErrfile(java.lang.String errfile)
错误输出文件
setFixBackslash(boolean fixBackslash)
URL中用/取代\
setForceOutput(boolean forceOutput)
不管生成的xml是否有错,强制输出。
setHideComments(boolean hideComments)
结果中不生成注释
setInputEncoding(java.lang.String encoding)
输入编码
setLogicalEmphasis(boolean logicalEmphasis)
用em替代i,strong替代b
setMessageListener(TidyMessageListener listener)
加入一个TidyMessageListener监听器
setOnlyErrors(boolean onlyErrors)
只输出错误文件
setOutputEncoding(java.lang.String encoding)
输出编码
setPrintBodyOnly(boolean bodyOnly)
只输出body中的部分
setRepeatedAttributes(int repeatedAttributes)
重复属性的处理
setSpaces(int spaces)
每行前的空格数,就是缩进格式
setTidyMark(boolean tidyMark)
是否生成tidy标记
setTrimEmptyElements(boolean trimEmpty)
不输出空元素
setUpperCaseAttrs(boolean upperCaseAttrs)
属性变大写
setUpperCaseTags(boolean upperCaseTags)
标记变大写
setWraplen(int wraplen)
多长换行
setXHTML(boolean xhtml)
输出xhtml(扩展性html)
setXmlOut(boolean xmlOut)
输出xml
setXmlPi(boolean xmlPi)
文件头输出xml标记
setXmlSpace(boolean xmlSpace)
加入xml名字空间属性
[/quote]

[color=green][b]如果你弄了半天还是被jtidy复杂的配置弄得晕头转向,你可以把他的 配置输出来看看 ,方法如下:
tidy.getConfiguration().printConfigOptions(new PrintWriter(System.out), true);[/b][/color]
附:[url]http://jtidy.sourceforge.net/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值