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"));