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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:59:13+00:00 2026-05-22T16:59:13+00:00

I am trying to unmarshal a 3rd party XML payload into a class. The

  • 0

I am trying to unmarshal a 3rd party XML payload into a class. The problem is that the payload has a parent/child relationship and the root node, the parent and the children all have the same element name. Here is a sample of the payload.

<?xml version="1.0" encoding="UTF-8"?>
<Directory>
    <id>2</id>
    <name>Media</name>
    <Directory>
        <id>5</id>
        <name>Default_Content</name>
        <Directory>
            <id>9</id>
            <name>Images</name>
        </Directory>
        <Directory>
            <id>8</id>
            <name>Icons</name>
        </Directory>
        <Directory>
            <id>6</id>
            <name>Additional_Content</name>
        </Directory>
    </Directory>
    <Directory>
        <id>12</id>
        <name>IC</name>
    </Directory>
</Directory>

So I am trying to annotate a class so JAXB/JAX-RS can unmarshal this into something useful.

I’ve tried something like this

@XmlRootElement(name="Directory")
public class Directory {
    private int id;
    private String name;

    @XmlElement(name="Directory");
    private List<Directory> directories = new ArrayList<Directory>();
}

But, predictably, it throws an IllegalAnnotationException because of having 2 properties with the same name.

Any ideas as to how I can use JAXB/JAX-RS to cleanly handle this mess or should I just parse it on my own?

  • 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-22T16:59:14+00:00Added an answer on May 22, 2026 at 4:59 pm

    Short Answer

    The exception is due to a field/property collision. You can either annotate the properties (get methods) or set the following annotation on your type:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Directory {
      ...
    }
    

    Long Answer

    JAXB’s default access type is PUBLIC_MEMBER this means that JAXB will map all public fields (instance variables) and properties (get/set methods).

    public class Foo {
    
        private String bar;
    
        public String getBar() {
            return bar;
        }
    
        public void setBar(String bar) {
            this.bar = bar;
        }
    
    }
    

    If you annotate a field:

    public class Foo {
    
        @XmlAttribute
        private String bar;
    
        public String getBar() {
            return bar;
        }
    
        public void setBar(String bar) {
            this.bar = bar;
        }
    
    }
    

    Then JAXB will think it has two bar properties mapped and thrown an exception:

    Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
    Class has two properties of the same name "bar"
        this problem is related to the following location:
            at public java.lang.String example.Foo.getBar()
            at example.Foo
        this problem is related to the following location:
            at private java.lang.String example.Foo.bar
            at example.Foo
    

    The solution is to annotate the property and set the XmlAccessType type to FIELD

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Foo {
    
        @XmlAttribute
        private String bar;
    
        public String getBar() {
            return bar;
        }
    
        public void setBar(String bar) {
            this.bar = bar;
        }
    
    }
    

    Your Model

    Directory

    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name="Directory")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Directory {
        private int id;
        private String name;
    
        @XmlElement(name="Directory")
        private List<Directory> directories = new ArrayList<Directory>();
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Directory> getDirectories() {
            return directories;
        }
    
        public void setDirectories(List<Directory> directories) {
            this.directories = directories;
        }
    
    }
    

    Demo

    import java.io.File;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(Directory.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            Directory directory = (Directory) unmarshaller.unmarshal(new File("input.xml"));
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(directory, System.out);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write functions that will allow me to marshal/unmarshal simple structs into
I'm trying to unmarshal a simple xml document from a public api from Convio.
I am trying to unmarshall a XML document created from jersey JAXB annotated class.
A one-element JSON array that I'm trying to unmarshal: [ { id:42, status:Active, name:purple
I'm trying to use JAXB to unmarshal this file into Java objects. I know
I'm upgrading a Java object that currently has XML representation in this spirit: <myObjects>
I'm trying to consume data from a 3rd party web service. Unfortunately, the web
I have some xml data I'm trying to unmarshall into java objects and one
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements

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.