Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:59:49+00:00 2026-06-10T04:59:49+00:00

I’m creating a program in Java which contains a tree-structure of objects. All classes

  • 0

I’m creating a program in Java which contains a tree-structure of objects. All classes involved implement the same interface, and each class contains a list of children:

public class MyClass1 implements MyInterface {    
    List<MyInterface> children;
}

public class MyClass2 implements MyInterface {    
    List<MyInterface> children;
}

public class MyClass3 implements MyInterface {    
    List<MyInterface> children;
}

....

Now the three structure is defined in an XML-file:

<myclass1>
    <myclass2></myclass2>
    <myclass1>
        <myclass3></myclass3>
    </myclass1>
</myclass1>

The tree structure can be of any type, defined by the user. Of course, each class contains class-specific variables which I omitted for now.

Now I’m trying to use XStream to deserialize the XML-file to Java-objects, preferably using annotations, but I don’t know how to do this. I don’t want to end up making a list for each possible class, e.g. in MyClass1:

@XStreamImplicit(itemFieldName="myclass2")
List<MyClass2> children2;
@XStreamImplicit(itemFieldName="myclass3")
List<MyClass3> children3;
@XStreamImplicit(itemFieldName="myclass4")
List<MyClass4> children4;

Any suggestions on how to solve this case with XStream? Or should I use other technologies?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:59:50+00:00Added an answer on June 10, 2026 at 4:59 am

    source.xml (Containing the XML we want to load):

    <myclass1>
        <myclass2></myclass2>
        <myclass1>
            <myclass3></myclass3>
        </myclass1>
    </myclass1>
    

    Interface:

    import java.util.List;
    
    public interface Myinterface {
        List<? extends Myinterface> getChildren();
    }    
    

    Abstract bean class (declaring children with @XStreamImplicit) :

    import java.util.ArrayList;
    import java.util.List;
    
    import com.thoughtworks.xstream.annotations.XStreamImplicit;
    
    public class MyAbstract implements Myinterface {
        @XStreamImplicit
        private List<? extends Myinterface> children = new ArrayList<Myinterface>();
    
    
        public List<? extends Myinterface> getChildren() {
            return children;
        }
    }
    

    Bean classes (extending MyAbstract, using @XStreamAlias to use the XML string you need):

    import com.thoughtworks.xstream.annotations.XStreamAlias;
    
    @XStreamAlias(value="myclass1")
    public class Myclass1 extends MyAbstract {
    
    }
    
    @XStreamAlias(value="myclass2")
    public class Myclass2 extends MyAbstract {
    
    }
    
    @XStreamAlias(value="myclass3")
    public class Myclass3 extends MyAbstract {
    
    }
    

    Main class (reading your XML source file. Don’t forget processAnnotations to load configuration from Annotated classes):

    import com.thoughtworks.xstream.XStream;
    
    public class XStreamTest {
        public static void main(String[] args) {
            XStream xStream = new XStream();
            xStream.processAnnotations(Myclass1.class);
            xStream.processAnnotations(Myclass2.class);
            xStream.processAnnotations(Myclass3.class);
    
            Object fromXML = xStream.fromXML(XStreamTest.class.getResourceAsStream("source.xml"));
            System.out.println(fromXML);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to select an H1 element which is the second-child in its group
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.