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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:34:39+00:00 2026-06-16T02:34:39+00:00

How can I instruct JAXB to process this ? XML <root> <parent> <child id=1

  • 0

How can I instruct JAXB to process this ?

XML

<root>
 <parent>
    <child id="1" name="foo" />
 </parent>
 <parent>
    <child id="3" name="foo2" />
 </parent>
 <parent>
    <child id="4" name="bar2" />
 </parent>
 <parent>
    <child id="2" name="bar" />
 </parent>
</root>

Root.java

@XmlRootElement
public class Root {
   @XmlElement(name="parent/child")
   List<Child> allChildren;
}

This doesn’t work … allChildren is empty.

  • 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-16T02:34:40+00:00Added an answer on June 16, 2026 at 2:34 am

    You could change your model and do the following:

    Root

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
       @XmlElement(name="parent")
       List<Parent> allParents;
    }
    

    Parent

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Parent {
       @XmlElement(name="child")
       List<Child> allChildren;
    }
    

    UPDATE

    Is it possible to avoid the parent class ?

    There are a couple of different ways to accomplish this:

    OPTION #1 – Any JAXB Implementation using XmlAdapter

    You could use an XmlAdapter to virtually add in the Parent class.

    ChildAdapter

    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class ChildAdapter extends XmlAdapter<ChildAdapter.Parent, Child> {
    
        public static class Parent {
            public Child child;
        }
    
        @Override
        public Parent marshal(Child v) throws Exception {
            Parent parent = new Parent();
            parent.child = v;
            return parent;
        }
    
        @Override
        public Child unmarshal(Parent v) throws Exception {
            return v.child;
        }
    
    }
    

    Root

    The @XmlJavaTypeAdapter annotation is used to reference the XmlAdapter.

    import java.util.List;
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
    
       @XmlElement(name="parent")
       @XmlJavaTypeAdapter(ChildAdapter.class)
       List<Child> allChildren;
    
    }
    

    Child

    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Child {
    
        @XmlAttribute
        int id;
    
        @XmlAttribute
        String name;
    
    }
    

    OPTION #2 – Using EclipseLink JAXB (MOXy)

    If you are using EclipseLink JAXB (MOXy) as your JAXB (JSR-222) implementation then you could do the following (Note: I’m the MOXy lead):

    Root

    import java.util.List;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Root {
    
       @XmlElement(name="parent")
       List<Child> allChildren;
    
    }
    

    Child

    MOXy’s @XmlPath annotation works pretty much the way you are trying to use the @XmlElement annotation in your post.

    import javax.xml.bind.annotation.*;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Child {
    
        @XmlPath("child/@id")
        int id;
    
        @XmlPath("child/@name")
        String name;
    
    }
    

    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

How can I instruct expect to look for something like Generating foo- [pattern] .tgz
Taking this basic example of jQuery Validate (see link below), how can you instruct
Currently, I can instruct Hyberjaxb to not persist a node in a xml object
Can anyone explain to me why this program: for(float i = -1; i <
is there a string in regular expressions that can instruct it to autoincrement it's
How can I instruct my Swing component to grab focus right now ? requestFocus()
Can someone instruct me how to save the state of the checkbox's of a
Is there a way that I can instruct WCF to accept JSON that is
How can I instruct the system to reload all kernel modules that have changed?
I'm having a problem. How can I instruct my XSLT to insert a new

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.