Abc & Def Abc & Def Abc & Def

Creating XMLStreamReader for a string with "&" character

196 views Asked by At

I want to unmarshall a xmlString which has '&' character in it.

    XMLInputFactory xif = XmlUtils.newXMLInputFactory();
    String test = "<Res>Abc & Def</Res>";
    StringReader sr = new StringReader(test);
    try {
      XMLStreamReader reader = xif.createXMLStreamReader(sr);
      JAXBContext jaxbContext = JAXBContext.newInstance(BluelineReprintInvoiceResponseData.class);
      Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
      Object o = unmarshaller.unmarshal(reader);
    } catch(Exception ex) {
      ex.printStackTrace();
    }

Is there any way to unmarshall the string without using replace/replaceAll to replace '&' character from the string?

1

There are 1 answers

0
Stephen C On

Is there any way to unmarshall the string without using replace / replaceAll to replace & characters in the string?

Basically no, because that is invalid XML. A bare ampersand is not permitted in that context. Any conformant XML parser will give you a parse error when it encounters that & and then doesn't find the corresponding ;

The correct solution it to fix the (so-called) XML at its source. What ever is generating that string as XML is faulty. If it is being entered by a human, the XML should be validated on entry.

Note that if you attempt to automatically "correct" bare ampersands in broken XML using string replacement, you risk doing other damage; i.e.

  • breaking character and entity references, or
  • messing up the content of CDATA sections.

The auto-correction code needs to be aware of the context in which the ampersand appears. It would be difficult to do using regexes.