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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:38:20+00:00 2026-05-20T07:38:20+00:00

I want to Generate XML that look like this : <mainNode> <node1></node1> <node2></node2> </mainNode>

  • 0

I want to Generate XML that look like this :

    <mainNode>   
       <node1></node1> 
       <node2></node2> 
    </mainNode>
   <mainNode2></mainNode2> 

and this is how i generate the mainNode1 , mainNode2 and node1 in my code:

   @XmlElementWrapper(name = "mainNode")
        @XmlElement(name = "node1")
        public List<String> getValue() {
            return value;
        }

   @XmlElement(name = "mainNode2")
   public String getValue2() {
   return value2;
   }

how i could add node2 to the mainNode1 ?

  • 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-20T07:38:21+00:00Added an answer on May 20, 2026 at 7:38 am

    You don’t seem to have a root element in your example. You could do something like this to obtain the structure you want:-

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement
    class Node {
        private MainNode    mainNode;
        private MainNode2   mainNode2;
    
        public Node() {
        }
    
        public Node(MainNode mainNode, MainNode2 mainNode2) {
            this.mainNode = mainNode;
            this.mainNode2 = mainNode2;
        }
    
    }
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement
    class MainNode {
        private String  node1;
        private String  node2;
    
        public MainNode() {
        }
    
        public MainNode(String node1, String node2) {
            this.node1 = node1;
            this.node2 = node2;
        }
    
    }
    
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlRootElement
    class MainNode2 {
    
    }
    

    Here’s my test code:-

    JAXBContext jc = JAXBContext.newInstance(Node.class);
    Marshaller m = jc.createMarshaller();
    
    MainNode mainNode = new MainNode("node1 value", "node2 value");
    MainNode2 mainNode2 = new MainNode2();
    Node node = new Node(mainNode, mainNode2);
    
    StringWriter sw = new StringWriter();
    
    m.marshal(node, sw);
    
    System.out.println(sw.toString());
    

    … and here’s the printout:-

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <node>
      <mainNode> 
        <node1>node1 value</node1>
        <node2>node2 value</node2>
      </mainNode>
      <mainNode2/>
    </node>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to generate the xml file that holds its schema as well as
I asked this question before: Generate XML from a class I want to do
I'm using this code in order to generate XML from my grid's cells values:
I want to generate XML Jaxb-classes (like here enter link description here ) from
I have an XML document that I want to generate unique IDs for. Some
I want to generate XML from existing xml but values should come from some
I've a file with xml data. And I want to generate a report out
I want to create an XSD which can generate the following XML. <note> <email:to>abc@def.com</email:to>
I want to generate a summary of the files that are in one tree
My xml looks like this <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price>

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.