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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:05:50+00:00 2026-06-06T03:05:50+00:00

Given the following XML structure: <root> <data>x</data> <details> <some_other_element>…</some_other_element> <collection> <element><a>1</a></element> <element><a>2</a></element> <element><a>3</a></element> <collection>

  • 0

Given the following XML structure:

<root>
  <data>x</data>
  <details>
    <some_other_element>...</some_other_element>
    <collection>
      <element><a>1</a></element>
      <element><a>2</a></element>
      <element><a>3</a></element>
    <collection>
  </details>
</root>

How would I unmarshal this into a mostly flat POJO using MOXy? I tried this approach (getters and setters left out for brevity):

@XmlRootElement(name = "root")
class Root {
  @XmlElement
  private String data;

  @XmlElement
  @XmlPath("details/some_other_element")
  private String someOtherElement;

  @XmlPath("details")
  @XmlElementWrapper(name = "collection")
  @XmlElement(name = "element")
  private Collection<Element> elements;
}

class Element {
  @XmlElement
  private String a;
}

Unfortunately, this just adds a single uninitialized to the elements collection. I’d like to avoid having to declare a Details class (which does indeed work) as that breaks the other @XmlPath mappings for elements below <details>.

I have verified that MOXy is actually loaded (as declared in the jaxb.properties).

Any ideas?

  • 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-06T03:05:53+00:00Added an answer on June 6, 2026 at 3:05 am

    Try adding the wrapping tag and the element to the @XmlPath instead of using @XmlElementWrapper:

    @XmlPath("details/collection/element")
    @XmlElement(name = "element")
    private Collection<Element> elements;
    

    I think you can even keep the @XmlElementWrapper annotation in case of future change of the jaxb provider.


    Below is the minimal annotations your require for your use case:

    package forum11153599;
    
    import java.util.Collection;
    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement
    class Root {
        @XmlElement
        private String data;
    
        @XmlPath("details/some_other_element/text()")
        private String someOtherElement;
    
        @XmlPath("details/collection/element")
        private Collection<Element> elements;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an input xml file with following structure: <root> <record row=1 col=1 val=1 />
I have following xml structure: //this is the root <factory ver=123 id=1> //can be
Given the following XML, I would like to return all eventtitles where the eventtype
Given the following XML: <root> Pacman <format bold=1 italic=1>rules</format>! </root> What is a better
The xml file is having the following structure <Root> <Child value=A/> <Child value=B/> <Child
Given the following XML structure: <courses> <course> <title>foo</title> <description>bar</description> </course> ... </courses> How could
Given the following XML (in an SQL column field called 'xfield'): <data> <section> <item
Given this XML structure from (http://localhost:3000/route.xml): <objects type=array> <object> <trip0> <departure>20:01</departure> <start>Place a</start> <end>Place
I have an XML structure like the following <root> <person> <name>James</name> <description xsi:type=me:age>12</description> <description
Given the following XML structure <html> <body> <div> <span>Test: Text2</span> </div> <div> <span>Test: Text3</span>

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.