开发者

SAXParseException when “ is used in XML

开发者 https://www.devze.com 2023-04-13 09:30 出处:网络
I\'m getting a \"org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 26; The entity \"ldquo\" was referenced, but not declared.\" exception when reading an XML document. I\'m reading it as fol

I'm getting a "org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 26; The entity "ldquo" was referenced, but not declared." exception when reading an XML document. I'm reading it as follows:

   开发者_高级运维             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();
                InputSource is = new InputSource(new StringReader(xmlBody));
                Document document = builder.parse(is);

And then there's an exception on builder.parse(is); From searching I figured that it is necessary to declare some of those new entities externally, unfortunately, I cannot modify the original XML document.

How do I fix this problem?

Thanks


From searching I figured that it is necessary to declare some of those new entities externally, unfortunately, I cannot modify the original XML document.

Well, unless you declare the entity then the document isn't XML and you won't be able to process it using an XML parser.

When you are asked to process input that isn't well-formed XML, the best approach is to fix the process that created the document (the whole idea of using XML for interchange relies on it being well-formed XML). The alternatives are to "repair" the document to turn it into well-formed XML (which you say you can't do), or to forget the fact that it was intended to be XML, and treat it as you would any proprietary non-XML format.

Not a pleasant set of choices - but that's the mess you get into when people pay lip-service to XML but fail to conform to the letter of the standard.


Try

factory.setExpandEntityReferences(false);

This will prevent the parser from trying to expand entities.

EDIT: How about this http://xerces.apache.org/xerces2-j/features.html#dom.create-entity-ref-nodes -- The top of that page has an example of how to set features on the underlying parser. This should cause the parser to create entity-reference DOM nodes instead of trying to expand the entities.

0

精彩评论

暂无评论...
验证码 换一张
取 消