序列化工作正常,但desirialisation wount工作。 我将发布我的代码和Eclipse中的错误副本。
<dependency> <!-- for XML -->
<artifactId>xstream</artifactId>
<groupId>com.thoughtworks.xstream</groupId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>xpp3</groupId>
<artifactId>xpp3</artifactId>
<version>1.1.4c</version>
</dependency>
代码:
public static void serialize(File file, Graph g) {
HashMap<String, List<String>> imageTMP = g.getImageURLmap();
HashMap<Coordinate, String> coordTMP = g.getCoordMap();
// ... and serialize it via XMLEncoder to file
XStream xStream = new XStream();
try {
FileOutputStream outputStream = new FileOutputStream(file);
OutputStream buff = new BufferedOutputStream(outputStream);
OutputStreamWriter writer = new OutputStreamWriter(buff, Charset.forName("UTF-8"));
xStream.toXML(imageTMP, writer);
xStream.toXML(coordTMP, outputStream);
writer.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void deserializeImage(File file, Graph g) {
HashMap<String, List<String>> imageTMP;
HashMap<Coordinate, String> coordTMP;
XStream xStream = new XStream();
try {
FileInputStream inputStream = new FileInputStream(file);
BufferedInputStream buff = new BufferedInputStream(inputStream);
InputStreamReader reader = new InputStreamReader(buff, Charset.forName("UTF-8"));
ObjectInputStream in = xStream.createObjectInputStream(inputStream);
imageTMP = (HashMap<String, List<String>>) in.readObject();
coordTMP = (HashMap<Coordinate, String>) in.readObject();
reader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
imageTMP = new HashMap<String, List<String>>();
coordTMP = new HashMap<Coordinate, String>();
}
g.setImageURLmap(imageTMP);
g.setCoordMap(coordTMP);
}
XML:
<map>
<entry>
<string>Netto</string>
<list/>
</entry>
<entry>
<string>Sparkasse</string>
<list>
<string>http://upload.wikimedia.org/wikipedia/commons/4/47/Salem_Rechnung.jpg</string>
<string>http://upload.wikimedia.org/wikipedia/de/8/83/Sparkasse.svg</string>
<string>http://upload.wikimedia.org/wikipedia/de/3/38/ErsteBank_Sparkasse_Logo.svg</string>
<string>http://upload.wikimedia.org/wikipedia/commons/8/88/Caixa_Manlleu_branch.JPG</string>
</list>
</entry>
<entry>
<string>Bohlmann und Meyer</string>
<list/>
</entry>
<entry>
<string>Langwedel</string>
<list/>
</entry>
</map><map>
<entry>
<com.vividsolutions.jts.geom.Coordinate>
<x>9.18013565</x>
<y>52.983609799999996</y>
<z>NaN</z>
</com.vividsolutions.jts.geom.Coordinate>
<string> </string>
</entry>
<entry>
<com.vividsolutions.jts.geom.Coordinate>
<x>9.1815044</x>
<y>52.9855979</y>
<z>NaN</z>
</com.vividsolutions.jts.geom.Coordinate>
<string> </string>
</entry>
</map>
使用新的XStream();我明白了:
com.thoughtworks.xstream.converters.ConversionException: Cannot construct java.util.Map$Entry : java.util.Map$Entry : Cannot construct java.util.Map$Entry : java.util.Map$Entry
---- Debugging information ----
message : Cannot construct java.util.Map$Entry : java.util.Map$Entry
cause-exception : com.thoughtworks.xstream.converters.reflection.ObjectAccessException
cause-message : Cannot construct java.util.Map$Entry : java.util.Map$Entry
class : java.util.Map$Entry
required-type : java.util.Map$Entry
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /entry
line number : 1
version : 1.4.7
-------------------------------
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1185)
at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1157)
at com.thoughtworks.xstream.XStream$4.readFromStream(XStream.java:1851)
at com.thoughtworks.xstream.core.util.CustomObjectInputStream.readObjectOverride(CustomObjectInputStream.java:120)
at java.io.ObjectInputStream.readObject(Unknown Source)
at org.opentripplanner.util.SeMo_Wiki.deserializeImage(SeMo_Wiki.java:255)
at org.opentripplanner.graph_builder.GraphBuilderTask.run(GraphBuilderTask.java:153)
at org.opentripplanner.standalone.OTPMain.main(OTPMain.java:61)
Caused by: com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Cannot construct java.util.Map$Entry : java.util.Map$Entry
at com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider.newInstance(SunLimitedUnsafeReflectionProvider.java:80)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(AbstractReflectionConverter.java:553)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:256)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
... 13 more
Caused by: java.lang.InstantiationException: java.util.Map$Entry
at sun.misc.Unsafe.allocateInstance(Native Method)
at com.thoughtworks.xstream.converters.reflection.SunLimitedUnsafeReflectionProvider.newInstance(SunLimitedUnsafeReflectionProvider.java:76)
... 16 more
使用新的XStream(新的DomDriver());
[Fatal Error] :127:8: Markup im Dokument nach dem Root-Element muss ordnungsgemäß formatiert sein.
com.thoughtworks.xstream.io.StreamException: : Markup im Dokument nach dem Root-Element muss ordnungsgemäß formatiert sein.
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:105)
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:77)
at com.thoughtworks.xstream.XStream.createObjectInputStream(XStream.java:1812)
at org.opentripplanner.util.SeMo_Wiki.deserializeImage(SeMo_Wiki.java:253)
at org.opentripplanner.graph_builder.GraphBuilderTask.run(GraphBuilderTask.java:153)
at org.opentripplanner.standalone.OTPMain.main(OTPMain.java:61)
Caused by: org.xml.sax.SAXParseException; lineNumber: 127; columnNumber: 8; Markup im Dokument nach dem Root-Element muss ordnungsgemäß formatiert sein.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:98)
... 5 more
有人对我有建议吗?我完全尝试了我能提到的一切?
即使只有简单的HashMap<Coordinate, String>
单独使用(HashMap<String, List<String>>
所以似乎不是泛型等问题......)
Greatings D.