0

将InputStream转换为Document时出现错误,我正在使用DocumentBuilder并按以下方式导入,但出现类型不兼容错误。

编码 :

import com.sun.xml.internal.txw2.Document; ////
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder; ////
import javax.xml.parsers.DocumentBuilderFactory; ////

public static Document parse(InputStream is) { // converting the resulted InputStream to a document

Document doc = null ;    
DocumentBuilderFactory domFactory;
DocumentBuilder builder;

try {
domFactory = DocumentBuilderFactory.newInstance();
domFactory.setValidating(false);
domFactory.setNamespaceAware(false);
builder = domFactory.newDocumentBuilder();
doc = builder.parse(is);  // I have the error here
} catch (Exception ex) {
System.err.println("unable to load XML: " + ex);
}

return doc;
}
4

1 回答 1

2

将 com.sun.xml.internal.trw2.Document 的导入更改为 org.w3c.dom.Document。

于 2013-05-17T19:35:05.033 回答