任何人都可以建議我將html轉換爲使用java的最佳方法 有沒有可用的API? 的HTML還可能包含JavaScript代碼使用java將html轉換爲xml
我曾嘗試下面的代碼:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
class HTML2XML {
public static void main(String args[]) throws JDOMException {
InputStream isInHtml = null;
URL url = null;
URLConnection connection = null;
DataInputStream disInHtml = null;
FileOutputStream fosOutHtml = null;
FileWriter fwOutXml = null;
FileReader frInHtml = null;
BufferedWriter bwOutXml = null;
BufferedReader brInHtml = null;
try {
// url = new URL("www.climb.co.jp");
// connection = url.openConnection();
// isInHtml = connection.getInputStream();
frInHtml = new FileReader("D:\\Second.html");
brInHtml = new BufferedReader(frInHtml);
SAXBuilder saxBuilder = new SAXBuilder(
"org.ccil.cowan.tagsoup.Parser", false);
org.jdom.Document jdomDocument = saxBuilder.build(brInHtml);
XMLOutputter outputter = new XMLOutputter();
org.jdom.output.Format newFormat = outputter.getFormat();
String encoding = "iso-8859-2";
newFormat.setEncoding(encoding);
outputter.setFormat(newFormat);
try {
outputter.output(jdomDocument, System.out);
fwOutXml = new FileWriter("D:\\Second.xml");
bwOutXml = new BufferedWriter(fwOutXml);
outputter.output(jdomDocument, bwOutXml);
System.out.flush();
} catch (IOException e) {
}
} catch (IOException e) {
} finally {
System.out.flush();
try {
isInHtml.close();
disInHtml.close();
fosOutHtml.flush();
fosOutHtml.getFD().sync();
fosOutHtml.close();
fwOutXml.flush();
fwOutXml.close();
bwOutXml.close();
} catch (Exception w) {
}
}
}
}
但它不是按預期工作
你的意思是XHTML?那麼這個Javascript代碼怎麼樣,你想要做什麼? – GolezTrol
我必須將普通html文件轉換爲xml – suresh
您是否需要將它們轉換爲* XHTML? XHTML是HTML的XML表示。 '只要'XML可以是任何東西。 – GolezTrol