0

I am trying to transform a .htm webpage into .xml file using JTidyand will need to extract some data/anchor element in .xml file. However, when doing the transforming step, it always results in a error file and tells me Warning: unknown attribute and Warning: <title> isn't allowed in <body> elements (the warnings in generated error file).

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();
}

thanks so much for your help and is there any better way to accomplish it? Really appreciate if providing some detailed code.

Zzz...
  • 291
  • 6
  • 19

1 Answers1

0

You could use XSLT to transform it http://www.w3schools.com/xml/xml_xsl.asp

Graeme
  • 1,107
  • 2
  • 12
  • 30