Search code examples
javaxmlxsdsax

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

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 '&',

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 :)


Solution

  • To have the character & in the text in an XML document, it must be written as the character entity & or be inside a CDATA section.