[英]Validating XML which has '& 'against XSD throws "The entity name must immediately follow the '&' in the entity reference"
I have a xml which contains '&' in the content, and i am trying to validate the xml aganist the xsd using the following code我有一个在内容中包含“&”的 xml,我正在尝试使用以下代码验证 xml ganist xsd
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(new StreamSource(xsd)); Validator validator = schema.newValidator(); validator.validate(new StreamSource(xml));
the code works fine when XML doesnt has a '&',当 XML 没有“&”时,代码工作正常,
but throws this error但抛出这个错误
"The entity name must immediately follow the '&' in the entity reference" when '&' is included.当包含 '&' 时,“实体名称必须紧跟在实体引用中的 '&' 之后”。
any suggestions to work around this are gladly accepted.很高兴接受任何解决此问题的建议。 thanks in advance!!
提前致谢!! :)
:)
EDIT: i Have been trying to use &, we have to use &
编辑:我一直在尝试使用 & 我们必须使用
&
solved :)解决了 :)
you need to escape special characters like &.你需要转义像 & 这样的特殊字符。
& is used to represent other special character like <: < & 用于表示其他特殊字符,如 <: <
replace & with &用 & 替换 &
To have the character &
in the text in an XML document, it must be written as the character entity &
要在 XML 文档的文本中包含字符
&
,它必须写为字符实体&
or be inside a CDATA
section .或在
CDATA
部分内。
The '&' is illegal in XML content. '&' 在 XML 内容中是非法的。 Are you allowed to change the XML?
是否允许更改 XML? You could wrap your problematic content with a CDATA section
您可以使用CDATA 部分包装有问题的内容
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.