1. ホーム
  2. java

[解決済み] com.thoughtworks.xstream.mapper.CannotResolveClassException

2022-02-17 02:48:10

質問

XStreamを試すのはこれが初めてです。 しかし、私のXMLファイルをパースしようとすると、この例外が発生します。

Exception in thread "main" com.thoughtworks.xstream.mapper.CannotResolveClassException: root
    at com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
    at com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
    at com.thoughtworks.xstream.core.util.HierarchicalStreams.readClassType(HierarchicalStreams.java:29)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:133)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1157)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1141)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1012)
    at com.mmm.transport.se.xmleditor.xml.TestXML.test(TestXML.java:20)
    at com.mmm.transport.se.xmleditor.domain.Main.main(Main.java:13)

つまり、エラーは私の testXML クラスの 20 行目にあることがわかります。そして、それは次のように見えます。

public class TestXML {

    public void test() throws FileNotFoundException {

        FileReader reader = new FileReader("xmlFiles/CoreDatamodel.xml");

        XStream xstream = new XStream();
        xstream.processAnnotations(Properties.class);
        xstream.processAnnotations(Parameters.class);
        xstream.processAnnotations(ObjType.class);
        xstream.processAnnotations(Type.class);
        Type data = (Type) xstream.fromXML(reader);

        System.out.println(data);
    }

}

そして20行目は、この行です。 xstream.processAnnotations(Parameters.class);

@XStreamAlias("param")
public class Parameters {

    @XStreamAlias("DATATYPE")
    private String datatype;
    @XStreamAlias("DESCRIPTION")
    private String description;
    @XStreamAlias("MIN_NO")
    private String min_no;
    @XStreamAlias("MAX_NO")
    private String max_no;
    @XStreamAlias("ORDER1")
    private String order1;
    @XStreamAlias("NESTED")
    private String nested;
    @XStreamAlias("DEFAULT1")
    private String default1;
    @XStreamAlias("FORMAT")
    private String format;
    @XStreamAlias("PARAMETER")
    private String parameter;

    public Parameters(String datatype, String description, String min_no,
            String max_no, String order1, String nested, String default1,
            String format, String parameter) {
        super();

        setDatatype(datatype);
        setDescription(description);
        setMin_no(min_no);
        setMax_no(max_no);
        setOrder1(order1);
        setNested(nested);
        setDefault1(default1);
        setFormat(format);
        setParameter(parameter);

    } //Getters and setters..

そして、xmlは次のようになります。

- <root>
- <info>
  <CORE_NAME>DataModel_Core</CORE_NAME> 
  <CORE_VERSION>..</CORE_VERSION> 
  <CORE_PRODUCT_ID>...</CORE_PRODUCT_ID> 
  <ADAPTATION_NAME /> 
  <ADAPTATION_VERSION /> 
  <ADAPTATION_PRODUCT_ID /> 
  </info>
- <type>
  <OBJECT_TYPE>data</OBJECT_TYPE> 
- <prop>
  <DESCRIPTION>Site parameters</DESCRIPTION> 
  <PARENT>NULL</PARENT> 
  <VIRTUAL>0</VIRTUAL> 
  <VISIBLE>1</VISIBLE> 
  <PICTURE>NULL</PICTURE> 
  <HELP>10008</HELP> 
  <MIN_NO>1</MIN_NO> 
  <MAX_NO>1</MAX_NO> 
  <NAME_FORMAT>NULL</NAME_FORMAT> 
  </prop>
- <param>
  <PARAMETER>nidRbc</PARAMETER> 
  <DATA_TYPE>INTEGER</DATA_TYPE> 
  <DESCRIPTION>RBC identity</DESCRIPTION> 
  <MIN_NO>1</MIN_NO> 
  <MAX_NO>1</MAX_NO> 
  <ORDER1>1</ORDER1> 
  <NESTED>0</NESTED> 
  <DEFAULT1>NULL</DEFAULT1> 
  <FORMAT>0:16382</FORMAT> 
  </param>
</type>

他のクラスは次のようになります。

public class Type {

    @XStreamImplicit(itemFieldName = "type")
    private List types = new ArrayList();

}

public class ObjType {

    @XStreamAlias("OBJECT_TYPE")
    private String objectType;

    public ObjType() {

    } // Getters and setters

@XStreamAlias("prop")
public class Properties {

    @XStreamAlias("DESCRIPTION")
    private String description;
    @XStreamAlias("PARENT")
    private String parent;
    @XStreamAlias("VIRTUAL")
    private String virtual;
    @XStreamAlias("VISIBLE")
    private String visible;
    @XStreamAlias("PICTURE")
    private String picture;
    @XStreamAlias("HELP")
    private String help;
    @XStreamAlias("MIN_NO")
    private String min_no;
    @XStreamAlias("MAX_NO")
    private String max_no;
    @XStreamAlias("NAME_FORMAT")
    private String name_format;
    public static Properties instance = null;

    public static Properties getInstance() {

        if (instance == null) {

            instance = new Properties();

        }

        return instance;
    }

    public Properties() {

    } // Getters and Setters.

このエラーの原因は何ですか?

解決方法は?

この例外が発生するのは、xmlのrootとinfoという要素に対応する表現がないためです。

そのため、XStream が xml ファイルを読み込む際には、ルート要素に一致するクラスを検索しています。もし、エイリアスやマッピングが定義されていなければ、タグ名をクラス名として試します。

だから、代わりに

public class Type {
    @XStreamImplicit(itemFieldName = "type")
    private List types = new ArrayList();
}

というような使い方をします。

@XStreamAlias("root")
public class Type {
    private Info info;

    @XStreamImplicit(itemFieldName = "type")
    private List types = new ArrayList();
}

private class Info {
    // ... define the attributes for the info element here.
}

参考までに、読みやすくするためにxmlをフォーマットしてみました。

<root>
    <info>
        <CORE_NAME>DataModel_Core</CORE_NAME> 
        <CORE_VERSION>..</CORE_VERSION> 
        <CORE_PRODUCT_ID>...</CORE_PRODUCT_ID> 
        <ADAPTATION_NAME /> 
        <ADAPTATION_VERSION /> 
        <ADAPTATION_PRODUCT_ID /> 
    </info>
    <type>
        <OBJECT_TYPE>data</OBJECT_TYPE>
        <prop>
            <DESCRIPTION>Site parameters</DESCRIPTION> 
            <PARENT>NULL</PARENT> 
            <VIRTUAL>0</VIRTUAL> 
            <VISIBLE>1</VISIBLE> 
            <PICTURE>NULL</PICTURE> 
            <HELP>10008</HELP> 
            <MIN_NO>1</MIN_NO> 
            <MAX_NO>1</MAX_NO> 
            <NAME_FORMAT>NULL</NAME_FORMAT> 
        </prop>
        <param>
            <PARAMETER>nidRbc</PARAMETER> 
            <DATA_TYPE>INTEGER</DATA_TYPE> 
            <DESCRIPTION>RBC identity</DESCRIPTION> 
            <MIN_NO>1</MIN_NO> 
            <MAX_NO>1</MAX_NO> 
            <ORDER1>1</ORDER1> 
            <NESTED>0</NESTED> 
            <DEFAULT1>NULL</DEFAULT1> 
            <FORMAT>0:16382</FORMAT> 
        </param>
    </type>
</root>