1

First time using XStream. Relatively new to Java as well. I've been trying to wrap my head around this for a few hours now and can't see why the following code won't work.

This is the error I am getting

Exception in thread "main" com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field com.helloworld.XMLExtractData.group
---- Debugging information ----
message             : No such field com.helloworld.XMLExtractData.group
field               : group
class               : com.helloworld.XMLExtractData
required-type       : com.helloworld.XMLExtractData
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /repository/group
line number         : 3
version             : 1.4.9
-------------------------------

XML

<?xml version="1.0" standalone="yes"?>
<repository>
    <group id="com.google">
        <artifact id="gson" type="jar">
            <version id="2.0.1">
                <dep spec="2.0,2.0"/>
            </version>
        </artifact>
        <artifact id="guava" type="jar">
            <version id="13.0.1">
                <dep spec="2.0,2.0"/>
            </version>
        </artifact>
    </group>
</repository>

My classes

import java.util.List;

public class XMLExtractData {
    public Repository repository;
}

class Repository {
    public List<Group> group;
}

class Group {
    public List<Artifact> artifact;
    public String id;
}

class Artifact {
    public List<Version> version;
    public String id;
    public String type;
}

class Version {
    public List<Dep> dep;
    public String id;
}

class Dep {
    public String spec;
}

Main

    XStream xstream = new XStream();
    xstream.alias("repository", XMLExtractData.class);
    xstream.alias("group", Group.class);
    //xstream.alias("artifact", Artifact.class);
    //xstream.alias("version", Version.class);
    //xstream.alias("dep", Dep.class);

    XMLExtractData indexXML = (XMLExtractData) xstream.fromXML(new URL(indexURL));

    indexXML.groups.stream().forEach((group) -> {
        System.out.println(group.id);
    });
user0000001
  • 2,092
  • 2
  • 20
  • 48
  • 1
    You should think of implicit collections. `Articfact` is implicit one. Then you have an attribute id. Look at this answer: http://stackoverflow.com/a/2533998/1370062 – RMachnik May 16 '16 at 20:09

0 Answers0