如何避免 com.thoughtworks.xstream.io.StreamException: 反序列化时无法创建 XmlPullParser?

How to avoid com.thoughtworks.xstream.io.StreamException: Cannot create XmlPullParser while deseriliazation?

反序列化 xml 时出现异常"Exception in thread "main" com.thoughtworks.xstream.io.StreamException: 无法创建 XmlPullParser"。我添加了以下依赖项

xmlpull_1_0_5.jar
xpp3_min-1.1.4.jar
xstream-1.4.8.jar

测试代码:

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class Test {

  public static void main(String[] args) {

    Map<String, String> map = new HashMap<String, String>();
    map.put("name", "chris");
    map.put("island", "faranga");

    XStream magicApi = new XStream();
    magicApi.registerConverter(new MapEntryConverter());
    magicApi.alias("root", Map.class);

    String xml = magicApi.toXML(map);
    System.out.println("Result of tweaked XStream toXml()");
    System.out.println(xml);

    magicApi.fromXML(xml);
    // assert extractedMap.get("name").equals("chris");
    // assert extractedMap.get("island").equals("faranga");

  }

  public static class MapEntryConverter implements Converter {

    public boolean canConvert(Class clazz) {
      return AbstractMap.class.isAssignableFrom(clazz);
    }

    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {

      AbstractMap map = (AbstractMap) value;
      for (Object obj : map.entrySet()) {
        Map.Entry entry = (Map.Entry) obj;
        writer.startNode(entry.getKey().toString());
        Object val = entry.getValue();
        if (null != val) {
          writer.setValue(val.toString());
        }
        writer.endNode();
      }

    }

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {

      Map<String, String> map = new HashMap<String, String>();

      while (reader.hasMoreChildren()) {
        reader.moveDown();

        String key = reader.getNodeName(); // nodeName aka element's name
        String value = reader.getValue();
        map.put(key, value);

        reader.moveUp();
      }

      return map;
    }

  }

}

我是否有一些依赖性。对此的任何帮助将不胜感激。

我添加了 xpp3_min-1.1.4c.jar 来代替 xpp3_min-1.1.4.jar。我的程序开始工作了

堆栈跟踪中未报告的根本问题是 xstream 1.4.8

中缺少资源

附上1.4.9源码(奇怪的是1.4.8源码没有),debug才能检测到

从未显示的真正错误是

caused by: org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available; nested exception is:
org.xmlpull.v1.XmlPullParserException: resource not found: /META-INF/services/org.xmlpull.v1.XmlPullParserFactory make sure that parser implementing XmlPull API is available
    at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:278)
    at org.xmlpull.v1.XmlPullParserFactory.newInstance(XmlPullParserFactory.java:259)
    at com.thoughtworks.xstream.io.xml.XppDriver.createParser(XppDriver.java:57)
    at com.thoughtworks.xstream.io.xml.AbstractXppDriver.createReader(AbstractXppDriver.java:54)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1061)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1052)
    at tudelft.mmi.pn.duo.core.DUOModel.loadFromXml(DUOModel.java:289)
    at tudelft.mmi.pn.duo.core.DUOModel.getNewDUOModel(DUOModel.java:182)
    at domaineditor.extduomodel.ExtDuoModel.<init>(ExtDuoModel.java:56)
    at domaineditor.actions.OpenFile.actionPerformed(OpenFile.java:34)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:157)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
    at java.awt.Component.processMouseEvent(Component.java:6533)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue.run(EventQueue.java:731)
    at java.awt.EventQueue.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

真正的潜在问题似乎是这个(已知)问题

https://issues.apache.org/jira/browse/SMX4-581

这个问题让我很头疼。我已经解决了使用:

XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6

如果问题仍然存在,请尝试导入这些

<dependency> 
        <groupId>com.thoughtworks.xstream</groupId> 
        <artifactId>xstream</artifactId> 
        <version>1.4</version> 
</dependency>

<dependency> 
       <groupId>xmlpull</groupId> 
       <artifactId>xmlpull</artifactId> 
       <version>1.1.3.1</version> 
</dependency> 
<dependency> 
       <groupId>xpp3</groupId> 
       <artifactId>xpp3_min</ artifactId> 
       <version>1.1.4c</version> 
</dependency>

参考文献:

https://x-stream.github.io/tutorial.html

https://my.oschina.net/u/3067322/blog/821900