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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:10:57+00:00 2026-06-07T00:10:57+00:00

I have this class: Class B { private String D; private String E; }

  • 0

I have this class:

Class B {
  private String D;
  private String E;
}

Using XStream, I would like to generate XML like this, where elements A and B are generated in the XML, even though they don’t exist in the java.:

<A>
        <B>
                <C>
                        <D/>
                        <E/>
                </C>
        </B>
</A>

Possible?

  • 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-07T00:10:58+00:00Added an answer on June 7, 2026 at 12:10 am

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    Since you are looking for an annotation based solution, you may be interested in the @XmlPath extension in MOXy.

    B

    The @XmlPath annotation allows you to specify your mapping as an XPath.

    package forum11334385;
    
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="A")
    @XmlAccessorType(XmlAccessType.FIELD)
    class B {
    
        @XmlPath("B/C/D/text()")
        private String D;
    
        @XmlPath("B/C/E/text()")
        private String E;
    
    }
    

    jaxb.properties

    To specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

    javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
    

    Demo

    package forum11334385;
    
    import java.io.File;
    import javax.xml.bind.*;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(B.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum11334385/input.xml");
            B b = (B) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(b, System.out);
        }
    
    }
    

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <A>
       <B>
          <C>
             <D>Foo</D>
             <E>Bar</E>
          </C>
       </B>
    </A>
    

    For More Information

    • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
    • http://blog.bdoughan.com/2011/08/binding-to-json-xml-geocode-example.html
    • http://blog.bdoughan.com/2011/03/map-to-element-based-on-attribute-value.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a simple class setup like this: class MyClass { private string
I have a Bean that looks like this Class Person{ private String name; private
i have a java class like this public class A { private String field1;
I have a class like this: class A { private: B* ptr; } But
I have a class User that looks like this: class User { private: char*
Suppose you have a simple class like this: class foo{ private: int* mData; int
Suppose I have a class like this: class Owner { private: long m_Id; QString
I have an array of function callbacks, like this: class Blah { private var
I have a class like this public class foo { private void getThread() {
I have this class: public class UploadFile : INotifyPropertyChanged { private string name; public

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.