我正在使用NekoHTML解析HTML文件的內容..如何在NekoHTML中將文檔對象的內容寫入字符串?
除了將文檔對象的內容提取到某個字符串以外,一切都會好起來的。
我已經嘗試使用
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(writer);
transformer.transform(source, result);
但似乎什麼都不會返回。
//this nekohtml
DOMParser parser = new DOMParser();
parser.parse(archivo);
//this xerces
OutputFormat format = new OutputFormat(parser.getDocument());
format.setIndenting(true);
//print xml for console
//XMLSerializer serializer = new XMLSerializer(System.out, format);
//save xml in string var
OutputStream outputStream = new ByteArrayOutputStream();
XMLSerializer serializer = new XMLSerializer(outputStream, format);
//process
serializer.serialize(parser.getDocument());
String xmlText = outputStream.toString();
System.out.println(xmlText);
//to generate a file output use fileoutputstream instead of system.out
//XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File("book.xml")), format);
地址:
請定義'作者'是什麼。 FileWriter可以用來寫入文件和StringWriter來寫入字符串緩衝區 – qwerty 2011-04-11 11:25:21
它是一個StringWriter – 2011-04-11 11:36:46