java解析xml时跳过文件头,在Java中解析XML文件时如何忽略内联DTD

I have a problem reading a XML file with DTD declaration inside (external declaration is solved). I'm using SAX method (javax.xml.parsers.SAXParser). When there is no DTD definition parsing looks like for example StartEement-Characters-StartElement-Characters-EndElement-Characters...... So there is characters method called immediately after Start or End element and thats how I need it to be. When DTD is in file parsing schema changes to for example StartElement-StartElement-StartElement-Characters-EndEement-EndEement-EndEement. And I need Characters method after every element. So I'm asking is there any way to prevent change of parsing schema?

My code:

SAXParserFactory factory = SAXParserFactory.newInstance();

factory.setValidating(false);

SAXParser parser = factory.newSAXParser();

XMLReader reader = parser.getXMLReader();

reader.setFeature("http://xml.org/sax/features/validation", false);

reader.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);

reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

reader.setFeature("http://xml.org/sax/features/external-general-entities", false);

reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);

reader.setFeature("http://xml.org/sax/features/use-entity-resolver2", false);

reader.setFeature("http://apache.org/xml/features/validation/unparsed-entity-checking", false);

reader.setFeature("http://xml.org/sax/features/resolve-dtd-uris", false);

reader.setFeature("http://apache.org/xml/features/validation/dynamic", false);

reader.setFeature("http://apache.org/xml/features/validation/schema/augment-psvi", false);

reader.parse(input);

There is XML file that I'm trying to parse link (its link on my dropbox).

解决方案

I suspect that the nodes that were previously being reported to the characters() callback are now being reported to the ignorableWhitespace() callback. The simplest solution might be to simply call characters() from ignorableWhitespace().

This is what the spec has to say about ignorableWhitespace():

Validating Parsers must use this method to report each chunk of

whitespace in element content (see the W3C XML 1.0 recommendation,

section 2.10): non-validating parsers may also use this method if they

are capable of parsing and using content models.

In other words, if there is a DTD, and if you are not validating, then

it's up to the parser whether it reports whitespace in element-only

content models using the characters() callback or the

ignorableWhitespace() callback.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值