如何使用xstream转换“&”?

时间:2018-10-15 03:45:13

标签: java xstream

当我尝试使用xstream转换器将我的VO更改为xml,然后将xml更改回我的VO时,它指出了以下错误:

这是原始的VO:“ A&B” 这是xml:“ A +%26 + B”

它将&转换为"%26",而不是"&"

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

我检查了VO对象后,发现有一个“ adr”字段,并且存储了以下值“ A&B”,当xstream转换器尝试将“ A +%26 + B”解析回我的VO时,它抛出上述异常,

有人可以教我如何使用xstream转换器处理“&”吗?

这就是我如何将VO转换为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;
        }

这就是我将xml转换为VO的方式

XStream xs = new XStream(new StaxDriver());
result = (PackedOtbOrderWhsInfo) xs.fromXML(java.net.URLDecoder.decode(msg.getMsg(), "UTF-8"));

0 个答案:

没有答案