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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:02:43+00:00 2026-05-29T11:02:43+00:00

I’m rather new to JAXB and have come across a problem which I can’t

  • 0

I’m rather new to JAXB and have come across a problem which I can’t seem to fix.

If I have a simple XML file as follows.

<Root>
    <Test>TestStuff</Test>
</Root>

I would like to create a class to handle “root” and another to handle “test”. I know normally you wouldn’t do this but for this example here’s the classes I created;

@XmlRootElement
class MyElement {
    private String test;
    @XmlElement(name="Test")
    public String getTest() { return test; }
    public void setTest(String test) { this.test = test; }  
}

@XmlRootElement(name="Root")
class Root {
    private MyElement myElement;
    public MyElement getMyElement() { return myElement; }
    public void setMyElement(MyElement myElement) { this.myElement = myElement; }
}

When I run the code below I always get null for myElement.

@Test
public void testJAXB() throws Exception {
    InputStream xmlInputStream = new FileInputStream(new File(".\\files\\test1.xml"));           
    JAXBContext context = JAXBContext.newInstance(Root.class, MyElement.class);
    Unmarshaller um = context.createUnmarshaller();
    Root root = (Root) um.unmarshal(xmlInputStream);

    Assert.assertTrue(root.getMyElement().getTest().equals("TestStuff"));          
}

Could someone point me in the right direction regarding this?
Thanks.

  • 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-29T11:02:43+00:00Added an answer on May 29, 2026 at 11:02 am

    You could do the following leveraging @XmlElement and @XmlValue:

    Root

    @XmlRootElement(name="Root")
    class Root {
        private MyElement myElement;
    
        @XmlElement(name="Test")
        public MyElement getMyElement() { return myElement; }
        public void setMyElement(MyElement myElement) { this.myElement = myElement; }
    }
    

    MyElement

    class MyElement {
        private String test;
    
        @XmlValue
        public String getTest() { return test; }
        public void setTest(String test) { this.test = test; }  
    }
    

    For More Information

    • http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html

    UPDATE

    If you are using EclipseLink MOXy (I’m the tech lead) as your JAXB provider you can use the @XmlPath(".") extension to map this use case.

    Root

    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="Root")
    class Root {
        private MyElement myElement;
    
        @XmlPath(".")
        public MyElement getMyElement() { return myElement; }
        public void setMyElement(MyElement myElement) { this.myElement = myElement; }
    }
    

    MyElement

    class MyElement {
        private String test;
    
        @XmlElement(name="Test")
        public String getTest() { return test; }
        public void setTest(String test) { this.test = test; }  
    }
    

    For More Information

    • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.