I want to parse an xml file to a java object.
I use XMLInputFactory library for doing this.
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = new FileInputStream(filepath);
eventReader = inputFactory.createXMLEventReader(in);
What can I do if I have an '&' in my text e.g.: <example>I like programming & football</example>
I always get an error message:
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[3608,52] Message: The entity name must immediately follow the '&' in the entity reference. at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
I know why the error is happening. The parser wants to look for &, &.., ... But I don't know how to escape the error.
Thanks.
The appearance of an unescaped (as &
) ampersand in an XML file violates the basic rule of how XML files have to be written. (<
and >
are other obvious examples.)
Any XML producer should make sure to adhere to the W3C spec for XML.
If you have to patch a badly written file and if & is not used for introducing HTML entities at all you might try a patch via sed or some similar utility.