简体   繁体   中英

Creating XMLStreamReader for a string with "&" character

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?

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; ie

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM