javax.xml.parsers.DocumentBuilder
在std:err
中打印消息。javax.xml.parsers.DocumentBuilder悄悄解析不可能?
實施例下面:
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class FooMain {
public static Document slurpXML(String s) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document rv = builder.parse(new ByteArrayInputStream(s.getBytes("UTF-8")));
return rv;
}
public static void main(String args[]) throws Exception {
try {
slurpXML("foo");
} catch (Exception e) {} // try to silence it - in vain
}
}
儘管try-catch塊,在命令行運行的程序產生:
$ java -classpath dist/foo.jar FooMain
[Fatal Error] :1:1: Content is not allowed in prolog.
我想在一個控制檯實用程序使用DocumentBuilder
和我不你不希望輸出混亂。有沒有辦法讓它沉默?
更通用的解決方案是重定向stderr – Taylor