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

123 views Asked by At

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

3

There are 3 answers

0
T.J. Crowder On BEST ANSWER

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.

0
fantarama On

you need to escape special characters like &.

& is used to represent other special character like <: &lt;

replace & with &amp;

0
Mario Trucco On

The '&' is illegal in XML content. Are you allowed to change the XML? You could wrap your problematic content with a CDATA section