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

  • Home
  • SEARCH
  • 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 3759168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:16:29+00:00 2026-05-19T10:16:29+00:00

I need to generate an XML like this: <Root> <Children> <InnerChildren>SomethingM</InnerChildren> </Children> </Root> The

  • 0

I need to generate an XML like this:

<Root>
   <Children>
      <InnerChildren>SomethingM</InnerChildren>
   </Children>
</Root>

The simplest solution is creating an inner class on the Root class:

@Root
class Root{
    @Element
    Children element;

    @Root
    private static class Children{
        @Element
        String innerChildren;
    }
}

But I want to avoid that inner class creation, since it will make things look strange while using Root objects. Is there anyway I can achieve that result without using inner classes?

Expected way to create Root objects:

Root root = new Root("Something");

What I want to avoid:

Children child = new Children("Something");
Root root = new Root(child);
// this could be achieve by injecting some annotations
// in the constructor, but it's awful
  • 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-19T10:16:30+00:00Added an answer on May 19, 2026 at 10:16 am

    Just use a normal class instead of an inner class. It should still work:

    @org.simpleframework.xml.Root
    public class Root{
        @Element
        Children children;
    
        public Root(){
            children = new Children("Something");
        }
    }
    
    @org.simpleframework.xml.Root
    public class Children{
        @Element
        String innerChildren;
    
        public Children(String inner){
            innerChildren = inner;
        }
    }
    

    Update:
    If you do not want to create another class, you can use the Path annotation by specifying an XPath expression for the innerChildren field. For example:

    @org.simpleframework.xml.Root
    class Root {
       @Element
       @Path("children")
       private final String innerChildren;
    
       public Root(String name){
           innerChildren = name;
       }
    }
    

    Produces:

    <root>
       <children>
          <innerChildren>Something</innerChildren>
       </children>
    </root>
    

    Use the Namespace annotation to add name spaces. For example:

    @org.simpleframework.xml.Root
    @Namespace(reference="http://domain/parent", prefix="bla")
    class Root {
       @Element
       @Path("bla:children")
       @Namespace(reference="http://domain/parent", prefix="bla")
       private final String innerChildren;
    
       public Root(String name){
           innerChildren = name;
       }
    }
    

    Produces:

    <bla:root xmlns:bla="http://domain/parent">
       <bla:children>
          <bla:innerChildren>Something</bla:innerChildren>
       </bla:children>
    </bla:root>
    

    If using Styles to format the XML, it’s necessary to do some modifications since they remove the : from the Element. The result using styles is:

    <bla:root xmlns:bla="http://domain/parent">
       <blachildren>
          <bla:innerChildren>Something</bla:innerChildren>
       </blachildren>
    </bla:root>
    

    This is what I did:

    public class MyStyle extends CamelCaseStyle{
        @Override
        public String getElement(String name) {
            if( name == null ){
                return null;
            }
            int index = name.indexOf(':');
            if( index != -1 ){
                String theRest = super.getElement(name.substring(index+1));
                return name.substring(0, index+1)+theRest;
            }
            return super.getElement(name);
        }
    }
    

    And now the result is the expected one:

    <bla:Root xmlns:bla="http://domain/parent">
       <bla:Children>
          <bla:InnerChildren>Something</bla:InnerChildren>
       </bla:Children>
    </bla:Root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate an xml for a feed which looks roughly like this
I need to generate XML in the following form: <ns1:root xmlns:ns1=http://example.com/xmlns1 xmlns:ns2=http://example.com/xmlns2> <ns2:item ns2:param=value
I need so generate XML like the following: <?xml version=1.0 encoding=UTF-8 standalone=yes?> <urlset xmlns=http://www.sitemaps.org/schemas/sitemap/0.9
how to do cut and paste in xslt? suppose my xml like this, <root>
I have an xml that looks like this: <Root> <tag1>4</tag1> <tag2>aa</tag2> <tag3 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema>
I need to generate XML on MonoTouch application ? How i can create like
I need to generate a soap request like this one. SOAP-REQUEST POST /TennisMasters/TennisMasters.Listener.asmx HTTP/1.1
What tool do I need to generate XML intellisense help file as shown below?
Flex has an issue with hyphens in xml. I need to generate an xml
I need to generate an editable xml file to supply content to a flash

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.