Как конвертировать "&" с помощью xstream? - PullRequest
0 голосов
/ 15 октября 2018

Когда я пытаюсь использовать конвертер 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 converter?

Вот как я конвертирую 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"));
...