0

When I try to use xstream converter to change my VO to xml and then change the xml back to my VO, it points out the following error:

This is the original VO : "A&B" This is the xml : "A+%26+B"

It converts & to "%26" instead of "&".

Error: com.thoughtworks.xstream.converters.ConversionException:  : ParseError at [row,col]:[1,3343]
    Message: The entity name must immediately follow the '&' in the entity reference. :  : ParseError at [row,col]:[1,3343]
    Message: The entity name must immediately follow the '&' in the entity reference.
    ---- Debugging information ----
    message             :  : ParseError at [row,col]:[1,3343]
    Message: The entity name must immediately follow the '&' in the entity reference.
    cause-exception     : com.thoughtworks.xstream.io.StreamException
    cause-message       :  : ParseError at [row,col]:[1,3343]
    Message: The entity name must immediately follow the '&' in the entity reference.
    class               : java.lang.String
    required-type       : java.lang.String
    converter-type      : com.thoughtworks.xstream.converters.SingleValueConverterWrapper
    wrapped-converter   : com.thoughtworks.xstream.converters.basic.StringConverter
    path                : /Data/shpList/ShipmentOrder/adrid
    line number         : 1
    class[1]            : com.threepltotal.broker.vo.wms.ShipmentOrder
    converter-type[1]   : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
    class[2]            : java.util.ArrayList
    converter-type[2]   : com.thoughtworks.xstream.converters.collections.CollectionConverter
    class[3]            : com.threepltotal.broker.vo.wms.OtbOrderInformation
    version             : null
    -------------------------------

After I check my VO object, it is found that there is an "adr" field and it stores the following value "A&B", when xstream converter try to parse "A+%26+B" back to my VO, it throws out the above exception,

May anyone teach me how to handle "&" using xstream converter?

This is how I convert VO to xml

public static String objectToXML(Object obj, String rootElementName) throws Exception {

            String xml = "";

            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            DocumentBuilder db=dbf.newDocumentBuilder();

            Document doc=db.newDocument();
            doc.setXmlStandalone(true);

            Element ele =objectToElement(obj, doc, rootElementName);
            doc.appendChild(ele);

            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();

            DOMSource source = new DOMSource(doc);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            StreamResult result =  new StreamResult(baos);

            transformer.transform(source, result);
            byte[] byteXML = baos.toByteArray();
            xml = new String(byteXML);

            return xml;
        }

And this is how I convert xml to my VO

XStream xs = new XStream(new StaxDriver());
result = (PackedOtbOrderWhsInfo) xs.fromXML(java.net.URLDecoder.decode(msg.getMsg(), "UTF-8"));
  • Take a look at https://stackoverflow.com/questions/8941911/xstream-is-converting-amp-to-when-converting-from-xml-to-java-objects and https://stackoverflow.com/questions/48141168/how-to-avoid-xstream-to-produce-xml-file-with-amp-or-quote-or-similar-chars – Balwinder Singh Oct 15 '18 at 03:52
  • 1
    Snippet is only work for **Javascript/HTML/CSS**,do not use it for java,it's hard to reading – flyingfox Oct 15 '18 at 03:54
  • Possible duplicate of [xstream is converting & to & when converting from xml to Java Objects](https://stackoverflow.com/questions/8941911/xstream-is-converting-amp-to-when-converting-from-xml-to-java-objects) – AsthaUndefined Oct 15 '18 at 04:11
  • thanks for the reminder and suggestion, but I find that xstream changes "&" to "%26" instead of "&", I have no idea why this happen. – Dennis Chan Oct 15 '18 at 04:14

0 Answers0