如何避免com.thoughtworks.xstream.io.StreamException:在进行deseriliazation时无法创建XmlPullParser?

时间:2015-04-26 18:52:55

标签: java xml xstream

我在反序列化xml时遇到异常“线程中的异常”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;
    }

  }

}

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

3 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:1)

堆栈跟踪中未报告的基本问题是xstream 1.4.8中缺少资源

只有附加1.4.9源代码(1.4.8源代码奇怪地不可用)并进行调试时才能检测到它。

从未显示的真正错误是

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.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.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$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.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

答案 2 :(得分:0)

这个问题让我非常头痛。我已经解决了:

| user_id |    dc_combo | dc_combo_size | dc_uid_cnt |             dn_combo | dn_combo_size | dn_uid_cnt | dn_combo_cnt |                        sn_combo | sn_combo_size | sn_uid_cnt | sn_combo_cnt |                                        dd_combo | dd_combo_size | dd_uid_cnt | dd_combo_cnt |
|---------|-------------|---------------|------------|----------------------|---------------|------------|--------------|---------------------------------|---------------|------------|--------------|-------------------------------------------------|---------------|------------|--------------|
|       3 |         200 |             1 |          2 |                AXIOM |             1 |          4 |            3 |                           nsaid |             1 |          4 |            3 |                                       nsaid10mg |             1 |          2 |            1 |
|     999 |         200 |             1 |          2 |                AXIOM |             1 |          4 |            3 |                           nsaid |             1 |          4 |            3 |                                       nsaid10mg |             1 |          2 |            1 |
|     175 |         300 |             1 |          1 |                AXIOM |             1 |          4 |            3 |                           nsaid |             1 |          4 |            3 |                                       nsaid25mg |             1 |          1 |            1 |
|       1 |          25 |             1 |          1 |                AXIOM |             1 |          4 |            3 |                           nsaid |             1 |          4 |            3 |                                        nsaid5mg |             1 |          1 |            1 |
|     999 |     200,350 |             2 |          1 |         AXIOM,BEXERA |             2 |          3 |            5 |                 nsaid,potassium |             2 |          3 |            5 |                         nsaid10mg,potassium12mg |             2 |          1 |            1 |
|     999 |  50,200,350 |             3 |          1 |         AXIOM,BEXERA |             2 |          3 |            5 |                 nsaid,potassium |             2 |          3 |            5 |           nsaid10mg,potassium12mg,potassium20mg |             3 |          1 |            1 |
|     999 |      50,200 |             2 |          1 |         AXIOM,BEXERA |             2 |          3 |            5 |                 nsaid,potassium |             2 |          3 |            5 |                         nsaid10mg,potassium20mg |             2 |          1 |            1 |
|     175 |      50,300 |             2 |          1 |         AXIOM,BEXERA |             2 |          3 |            5 |                 nsaid,potassium |             2 |          3 |            5 |                         nsaid25mg,potassium20mg |             2 |          1 |            1 |
|       1 |       25,50 |             2 |          1 |         AXIOM,BEXERA |             2 |          3 |            5 |                 nsaid,potassium |             2 |          3 |            5 |                          nsaid5mg,potassium20mg |             2 |          1 |            1 |
|     999 | 100,200,350 |             3 |          1 | AXIOM,BEXERA,CAZERTA |             3 |          2 |            3 | nsaid,potassium,sodium chloride |             3 |          2 |            3 |     nsaid10mg,potassium12mg,sodium chloride10mg |             3 |          1 |            1 |
|     999 |  50,100,200 |             3 |          1 | AXIOM,BEXERA,CAZERTA |             3 |          2 |            3 | nsaid,potassium,sodium chloride |             3 |          2 |            3 |     nsaid10mg,potassium20mg,sodium chloride10mg |             3 |          1 |            1 |
|       1 |   25,50,100 |             3 |          1 | AXIOM,BEXERA,CAZERTA |             3 |          2 |            3 | nsaid,potassium,sodium chloride |             3 |          2 |            3 |      nsaid5mg,potassium20mg,sodium chloride10mg |             3 |          1 |            1 |
|     999 |     100,200 |             2 |          1 |        AXIOM,CAZERTA |             2 |          2 |            2 |           nsaid,sodium chloride |             2 |          2 |            2 |                   nsaid10mg,sodium chloride10mg |             2 |          1 |            1 |
|       1 |      25,100 |             2 |          1 |        AXIOM,CAZERTA |             2 |          2 |            2 |           nsaid,sodium chloride |             2 |          2 |            2 |                    nsaid5mg,sodium chloride10mg |             2 |          1 |            1 |
|     201 |         350 |             1 |          2 |               BEXERA |             1 |          5 |            4 |                       potassium |             1 |          5 |            4 |                                   potassium12mg |             1 |          2 |            1 |
|     999 |         350 |             1 |          2 |               BEXERA |             1 |          5 |            4 |                       potassium |             1 |          5 |            4 |                                   potassium12mg |             1 |          2 |            1 |
|     999 |      50,350 |             2 |          1 |               BEXERA |             1 |          5 |            4 |                       potassium |             1 |          5 |            4 |                     potassium12mg,potassium20mg |             2 |          1 |            1 |
|     378 |         400 |             1 |          1 |               BEXERA |             1 |          5 |            4 |                       potassium |             1 |          5 |            4 |                                   potassium15mg |             1 |          1 |            1 |
|       1 |          50 |             1 |          3 |               BEXERA |             1 |          5 |            4 |                       potassium |             1 |          5 |            4 |                                   potassium20mg |             1 |          3 |            1 |
|     175 |          50 |             1 |          3 |               BEXERA |             1 |          5 |            4 |                       potassium |             1 |          5 |            4 |                                   potassium20mg |             1 |          3 |            1 |
|     999 |          50 |             1 |          3 |               BEXERA |             1 |          5 |            4 |                       potassium |             1 |          5 |            4 |                                   potassium20mg |             1 |          3 |            1 |
|     999 |  50,100,350 |             3 |          1 |       BEXERA,CAZERTA |             2 |          4 |            5 |       potassium,sodium chloride |             2 |          4 |            5 | potassium12mg,potassium20mg,sodium chloride10mg |             3 |          1 |            1 |
|     999 |     100,350 |             2 |          1 |       BEXERA,CAZERTA |             2 |          4 |            5 |       potassium,sodium chloride |             2 |          4 |            5 |               potassium12mg,sodium chloride10mg |             2 |          1 |            1 |
|     201 |     350,450 |             2 |          1 |       BEXERA,CAZERTA |             2 |          4 |            5 |       potassium,sodium chloride |             2 |          4 |            5 |               potassium12mg,sodium chloride30mg |             2 |          1 |            1 |
|     378 |     100,400 |             2 |          1 |       BEXERA,CAZERTA |             2 |          4 |            5 |       potassium,sodium chloride |             2 |          4 |            5 |               potassium15mg,sodium chloride10mg |             2 |          1 |            1 |
|       1 |      50,100 |             2 |          2 |       BEXERA,CAZERTA |             2 |          4 |            5 |       potassium,sodium chloride |             2 |          4 |            5 |               potassium20mg,sodium chloride10mg |             2 |          2 |            1 |
|     999 |      50,100 |             2 |          2 |       BEXERA,CAZERTA |             2 |          4 |            5 |       potassium,sodium chloride |             2 |          4 |            5 |               potassium20mg,sodium chloride10mg |             2 |          2 |            1 |
|       1 |         100 |             1 |          3 |              CAZERTA |             1 |          4 |            2 |                 sodium chloride |             1 |          4 |            2 |                             sodium chloride10mg |             1 |          3 |            1 |
|     378 |         100 |             1 |          3 |              CAZERTA |             1 |          4 |            2 |                 sodium chloride |             1 |          4 |            2 |                             sodium chloride10mg |             1 |          3 |            1 |
|     999 |         100 |             1 |          3 |              CAZERTA |             1 |          4 |            2 |                 sodium chloride |             1 |          4 |            2 |                             sodium chloride10mg |             1 |          3 |            1 |
|     201 |         450 |             1 |          1 |              CAZERTA |             1 |          4 |            2 |                 sodium chloride |             1 |          4 |            2 |                             sodium chloride30mg |             1 |          1 |            1 |

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

-- generalized membership

type Set a = [a]

member :: Eq a => a -> [a] -> Bool
member x []                 = False
member x (y:ys) | x == y    = True
                | otherwise = member x ys                

-- elimination of duplicates

noReps :: Set Integer -> Set Integer
noReps []                   = []
noReps (x:xs) | member x xs = noReps xs
              | otherwise   = x : noReps xs

inclusion :: Set Integer -> Set Integer -> Bool
inclusion x y = length [i | i <- x, not (member i y)] == 0

identical :: Set Integer -> Set Integer -> Bool 
identical x y = inclusion x y && inclusion y x

-- membership for sets of sets

setmember :: Set Integer -> Set (Set Integer) -> Bool
setmember [] _                      = True
setmember _ []                      = False
setmember x (y:ys)  | identical x y = True 
                    | otherwise     = setmember x ys   

addsets :: Set Integer -> Set (Set Integer) -> Set (Set Integer)
addsets x y | setmember x y = y
            | otherwise = x:y

-- adding an integer to every member of a sets of sets of integers

addelem :: Integer -> Set (Set Integer) -> Set (Set Integer) 
addelem x y = [noReps (x : i) | i <- y]

-- powerset of the set {1,...,n}

powerset :: Integer -> Set (Set Integer)
powerset 1 = [[], [1]]
powerset x = powerset (x-1) ++ addelem x (powerset (x-1))

参考:

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

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