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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:56:58+00:00 2026-06-16T13:56:58+00:00

I have an XML file like below, and I want to convert it into

  • 0

I have an XML file like below, and I want to convert it into a Java object.

<P1>
    <CTS>
        Hello
    </CTS>
    <CTS>
        World
    </CTS>
<P1>    

So I created the following Java classes with their properties.

P1 class

@XmlRootElement
public class P1 {
    @XmlElement(name = "CTS")
    List<CTS> cts;
}

CTS class

public class CTS {
    String ct;
}

Test Class

File file = new File("D:\\ContentTemp.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);
     
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            P1 p = (P1) jaxbUnmarshaller.unmarshal(file);

But I am getting the following error:

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:
2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "cts"

  • 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-16T13:56:59+00:00Added an answer on June 16, 2026 at 1:56 pm

    com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:
    2 counts of IllegalAnnotationExceptions
    Class has two properties of the same name "cts"

    By default a JAXB (JSR-222) implementation creates mappings based on properties and annotated fields. When you annotate a field for which there is also a property it will cause this error.

    You have two options:

    1. Use @XmlAccessorType(XmlAccessType.FIELD)

      You could annotate the field you need to specify @XmlAccessorType(XmlAccessType.FIELD) on the class.

      @XmlRootElement(name="P1)
      @XmlAccessorType(XmlAccessType.FIELD)
      public class P1 {
      
          @XmlElement(name = "CTS")
          List<CTS> cts;
      
      }
      
    2. Option #2 – Annotate the Property (get method)**

      Alternatively you could annotate the get method.

      @XmlRootElement(name="P1)
      public class P1 {
      
          List<CTS> cts;
      
          @XmlElement(name = "CTS")
          public List<CTS> getCts() {
              return cts;
          }
      
      }
      

    For More Information

    • http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html

    FULL EXAMPLE

    CTS

    You can use the @XmlValue annotation to map to Java class to a complex type with simple content.

    @XmlAccessorType(XmlAccessType.FIELD)
    public class CTS {
    
        @XmlValue
        String ct;
    
    }
    

    P1

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

    Demo

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

    input.xml/Output

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <P1>
        <CTS>
            Hello
        </CTS>
        <CTS>
            World
        </CTS>
    </P1>
    

    For More Information

    • http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml file like down below. I want to get pharmacies nodes'
I have a simple but huge xml file like below. I want to parse
i have xml file like below.I want to find specific text values in different
i have xml file like Below and Device class and i want get List<Device>
I have a sample xml file which looks like the one below: <Root> <SubOne>
I have an XML file which has many section like the one below: <Operations>
I have created an xml file like this: <?xml version=1.0 encoding=utf-8?> <ListView xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent
Let's say I have an empty XML file like so: <root></root> And I want
I have a simple file XML like below: <brandName type=http://example.com/codes/bmw# abbrev=BMW value=BMW />BMW</brandName> <maxspeed>
I have a XML file like the below: <clients> <client> <id>YYYY</id> <name>XXXX</name> <desc>ZZZZ</desc> <trade_info>

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.