java如何验证xml_如何使用java验证XML?

您需要在SAX解析器上使用本地实体解析器,以下是如何实现它的示例:

class LocalEntityResolver implements EntityResolver {

private static final Logger LOG = ESAPI.getLogger(LocalEntityResolver.class);

private static final Map DTDS;

static {

DTDS = new HashMap();

DTDS.put("-//W3C//DTD XHTML 1.0 Transitional//EN",

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd");

DTDS.put("-//W3C//ENTITIES Latin 1 for XHTML//EN",

"http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent");

DTDS.put("-//W3C//ENTITIES Symbols for XHTML//EN",

"http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent");

DTDS.put("-//W3C//ENTITIES Special for XHTML//EN",

"http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent");

}

@Override

public InputSource resolveEntity(String publicId, String systemId)

throws SAXException, IOException {

InputSource input_source = null;

if (publicId != null && DTDS.containsKey(publicId)) {

LOG.debug(Logger.EVENT_SUCCESS, "Looking for local copy of [" + publicId + "]");

final String dtd_system_id = DTDS.get(publicId);

final String file_name = dtd_system_id.substring(

dtd_system_id.lastIndexOf('/') + 1, dtd_system_id.length());

InputStream input_stream = FileUtil.readStreamFromClasspath(

file_name, "your/dtd/location",

getClass().getClassLoader());

if (input_stream != null) {

LOG.debug(Logger.EVENT_SUCCESS, "Found local file [" + file_name + "]!");

input_source = new InputSource(input_stream);

}

}

return input_source;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值