【问题标题】:parse .htm file/url into .xml file将 .htm 文件/url 解析为 .xml 文件
【发布时间】:2014-04-18 22:06:29
【问题描述】:

我正在尝试使用JTidy将 .htm 网页转换为 .xml 文件,并且需要在 .xml 文件中提取一些数据/锚元素。但是,在执行转换步骤时,它总是会导致错误文件并告诉我Warning: unknown attributeWarning: <title> isn't allowed in <body> elements(生成的错误文件中的警告)。

private String url; 
private String outFileName; 
private String errOutFileName; 

public Test(String url, String outFileName, String errOutFileName) { 
    this.url = url; 
    this.outFileName = outFileName; 
    this.errOutFileName = errOutFileName; 
}
public void convert() { 
    URL u; 
    BufferedInputStream in; 
    FileOutputStream out; 

    Tidy tidy = new Tidy(); 

    tidy.setXmlOut(true); 

    try { 
        //Set file for error messages
        tidy.setErrout(new PrintWriter(new FileWriter(errOutFileName), true)); 
        u = new URL(url); 

        //input and output streams
        in = new BufferedInputStream(u.openStream()); 
        out = new FileOutputStream(outFileName); 

        //Convert files
        tidy.parse(in, out); 

        in.close();
        out.close();

    } catch (IOException e) { 
        System.out.println(this.toString() + e.toString()); 
    } 
} 

public static void main(String[] args) {
    // Test(url address, correctOutput file directory, errorOuput file)
    Test t = new Test("here is the http.....", "e:/...../correctOutput.xml", "e:/...../errorOutput.xml");
    t.convert();
}

非常感谢您的帮助,有没有更好的方法来完成它?非常感谢提供一些详细的代码。

【问题讨论】:

  • 听起来 html 格式错误。

标签: java xml eclipse tidy


【解决方案1】:

您可以使用 XSLT 对其进行转换 http://www.w3schools.com/xml/xml_xsl.asp

【讨论】:

  • 谢谢,但我正在尝试将 html 转换为 xml 文档。 xslt 也可以吗?
猜你喜欢
  • 1970-01-01
  • 2014-10-24
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
相关资源
最近更新 更多