会报错,呵呵,说DTD不存在,我想搞过这个的一定都遇到这样的方法.
如何解决呢?
新建一个类
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* 取消DTD的验证
* @author sunlei
*
*/
public class NullEntityResolver implements EntityResolver {
static String emptyDtd = "";
static ByteArrayInputStream byteIs = new ByteArrayInputStream(emptyDtd.getBytes());
/**
* @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
*/
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(byteIs);
}
}
然后在你读取xml的时候
NullEntityResolver resolver = new NullEntityResolver();
xmlReader.setEntityResolver(resolver);
doc = xmlReader.read(file);
呵呵,在read前加上这两行.嗯,问题顺利解决.
就是这么简单.
支持原创啊. 转载请标明出处 http://www.chinacsharp.net
http://blog.csdn.net/keyboardsun 作者 keyboardsun