posts - 431,  comments - 344,  trackbacks - 0

要这么做是因为Server返回给我们的XML肯定是合法的,不需要验证。

而设置不需要验证,只需要设置DocumentBuilderFactory.setValidating(false)就可以达到效果了,但是解析器还是会读取DTD的,解决的方法是实现EntityResolver接口,具体代码如下:

package com.founder.demo;

import Java.io.ByteArrayInputStream;
import Java.io.IOException;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class IgnoreDTDEntityResolver implements EntityResolver {

 @Override
 public InputSource resolveEntity(String publicId, String systemId)
   throws SAXException, IOException {
        return new InputSource(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes()));
 }

}

然后设置SAXReader 对象如下:
SAXReader reader = new SAXReader();
reader.setEntityResolver(new IgnoreDTDEntityResolver()); // ignore dtd

一切ok。

posted on 2009-08-06 11:17 周锐 阅读(1837) 评论(2)  编辑  收藏 所属分类: JavaXML

只有注册用户登录后才能发表评论。


网站导航: