验证 XML 有 '& ' 反对 XSD 抛出 "The entity name must immediately follow the '&' in the entity reference"

Validating XML which has '& 'against XSD throws "The entity name must immediately follow the '&' in the entity reference"

我有一个 xml,内容中包含“&”,我正在尝试使用以下代码xsd 验证 xml 反对者 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));

当 XML 没有 '&' 时代码工作正常,

但是抛出这个错误

"The entity name must immediately follow the '&' in the entity reference" 当包含“&”时。

我们很乐意接受解决此问题的任何建议。 提前致谢!! :)

编辑: 我一直在尝试使用&,我们不得不使用& 解决了:)

您需要转义特殊字符,例如 &.

& 用于表示其他特殊字符,如 <: <

将 & 替换为 &

“&”在 XML 内容中是非法的。您可以更改 XML 吗?您可以用 CDATA section

包装有问题的内容

要在 XML 文档的文本中包含字符 &,它必须写成 the character entity &amp; or be inside a CDATA section