# The entity name must immediately follow the '&' in the entity reference

Source: https://xmlvalid.com/errors/entity-name-must-immediately-follow-the-ampersand

> A bare ampersand (&amp;) in text or in an attribute.

The message, as Java reports it: `The entity name must immediately follow the '&' in the entity reference.`

## Why it happens

- An unescaped `&` in text, such as a company name (`Smith & Sons`) or a URL query string.
- XML built by string concatenation instead of an XML writer, which would escape it.

## How to fix it

- Write `&amp;` for every literal ampersand, or put the text in a CDATA section. Build XML with an XML API (DOM, StAX, JAXB) and the escaping is done for you.
- Java's schema validation (`javax.xml.validation.Validator`, and JAXB or Spring on top of it) throws at the first error unless you set an `ErrorHandler` whose `error()` records the `SAXParseException` and carries on; each one has `getLineNumber()` and `getColumnNumber()`. A well-formedness error ends parsing whatever the handler does.

## The same problem in xmlvalid

libxml2, which xmlvalid validates with, reports it as [xmlParseEntityRef: no name](https://xmlvalid.com/errors/xmlparseentityref-no-name): `xmlParseEntityRef: no name`

## Example that fails

```xml
<company>Smith & Sons</company>
```

## Fixed

```xml
<company>Smith &amp; Sons</company>
```
