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 6928419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:11:07+00:00 2026-05-27T11:11:07+00:00

xml: <Node name=1> <Node name=2> <Node name=4></Node> </Node> <Node name = 3> <Node name=5></Node>

  • 0

xml:

<Node name="1">
   <Node name="2">
       <Node name="4"></Node>
   </Node>
   <Node name = "3">
       <Node name="5"></Node>
    </Node>
</Node>

I want to create the following object in java

Node{
 String name;
 List<Node> nodeList
}

Is there any xml parsing library which could do this.
I have tried xstream and simple, but have not been able to figure it out.

Any help would be appreciated.

  • 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-05-27T11:11:07+00:00Added an answer on May 27, 2026 at 11:11 am

    This code uses XStream and generates the output you are looking for.

    The node class:

    import java.util.ArrayList;
    import java.util.List;
    
    import com.thoughtworks.xstream.annotations.XStreamAlias;
    import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
    import com.thoughtworks.xstream.annotations.XStreamImplicit;
    
    @XStreamAlias("Node")
    public class Node {
        @XStreamAsAttribute
        private String name;
    
        @XStreamImplicit
        private List<Node> nodes = new ArrayList<Node>();
    
        public Node(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Node> getNodes() {
            return nodes;
        }
    
        public void setNodes(List<Node> nodes) {
            this.nodes = nodes;
        }
    
        public void addNode(Node n) {
            nodes.add(n);
        }
    }
    

    The Main class:

    import com.thoughtworks.xstream.XStream;
    
    
    public class NodeXStream {
    
        public static void main(String[] args) {
            Node n1 = new Node("1");
            Node n2 = new Node("2");
            Node n3 = new Node("3");
            Node n4 = new Node("4");
            Node n5 = new Node("5");
    
            n1.addNode(n2);
            n1.addNode(n3);
    
            n2.addNode(n4);
            n3.addNode(n5);
    
    
            XStream xs = new XStream();
            xs.processAnnotations(Node.class);
    
            // To XML
            String myXML = xs.toXML(n1);
    
            // From XML
            Node newNode = (Node) xs.fromXML(myXML);
    
        }
    
    }
    

    EDIT:
    Added the deserialization code.

    To deserialize you’ll also need to add the XPP3 library to the build path. It is part of XStream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple situation... With any random XML file, I want to create a list of
For example,I want to generate the following xml file by using java with DOM
I have a xml with the following structure: <NODES> <NODE> <NAME> </NAME> <IMG> </IMG>
I have an xml document where an xml node with a particular name, say
I want to read an specific xml node and its value for example <customers>
I need to have following attribute value in my XML node: CommandLine=copy $(TargetPath) ..\..\&#x0D;&#x0A;echo
I have an obeservable collection _kidlist and I want to create an XML doc
I have an XML document with companies listed in it. I want to create
I want to read XML data using XPath in Java, so for the information
I have the following YAML: - name: List of monkeys - author: Nicolas Raoul

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.