简体   繁体   English

针对 XSD 验证具有“&”的 XML 抛出“实体名称必须紧跟实体引用中的“&””

[英]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 &amp, we have to use &编辑:我一直在尝试使用 & 我们必须使用& solved :)解决了 :)

you need to escape special characters like &.你需要转义像 & 这样的特殊字符。

& is used to represent other special character like <: &lt; & 用于表示其他特殊字符,如 <: &lt;

replace & with &amp;用 & 替换 &

To have the character & in the text in an XML document, it must be written as the character entity &amp;要在 XML 文档的文本中包含字符& ,它必须写为字符实体&amp; 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.

相关问题 为什么实体名称必须紧跟在为XML出现的实体引用中的“&”后面? - Why must the entity name immediately follow the '&' in the entity reference appearing for the XML? 错误:“实体名称必须紧随实体引用中的“&”之后。” - ERROR: 'The entity name must immediately follow the '&' in the entity reference.' org.xml.sax.SAXParseException:当tomcat服务器在eclipse中运行时,实体名称必须紧随实体引用中的“&” - org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference when tomcat server run in eclipse 获取实体名称必须紧跟在java中的实体引用错误中的&#39;&&#39;,但我的xml文件中没有任何&符号 - Getting a The entity name must immediately follow the '&' in the entity reference error in java, but I dont have any ampersands in my xml file 针对具有包含项的xsd验证xml - Validating xml against an xsd which has an include MapReduce程序中的XMLParser失败,并显示以下消息:实体名称必须紧随实体引用中的“&”之后。 - XMLParser in MapReduce Program failing with the Message: The entity name must immediately follow the '&' in the entity reference. 使用XMLReader针对XSD验证XML - Validating XML against XSD with XMLReader 针对.xsd文件验证.xml的XML错误 - XML error validating .xml against .xsd file 麻烦验证XML对XSD(java) - troubles validating XML against XSD (java) 针对用于生成Bean的xsd验证xml - Validating an xml against xsd that was used to generate Beans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM