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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:48:20+00:00 2026-06-08T11:48:20+00:00

I have an XML String with an attribute that contains integer value: <item status=2

  • 0

I have an XML String with an attribute that contains integer value:

<item status="2" />

There is also Simple Framework class that describes this item:

@Root
public static class Item {

    @Attribute(name="status")
    private int status;

    public int getStatus() {
        return status;
    }

}

Unserialization works well, however I want to be able to change int type to the defined enum type.

public enum Status {
    OK(0), PENDING(1), ERROR(2);

    BetStatus(int status) {
        this.status = status;
    }

    public int getStatus() {
        return status;
    }
}

So with a quick modification:

@Root
public static class Item {

    @Attribute(name="status")
    private Status status;

    public Status getStatus() {
        return status;
    }

}

However now I get an error:

java.lang.IllegalArgumentException: 2 is not a constant in com.my.package.Status

Is it possible to cast int this way during unserialization ?

I bet I have to add some magic method to my Status class.

Solution:

According to Reimeus answer I leaved int type for status attribute and I modified just Item class with:

@Root
public static class Item {

    @Attribute(name="status")
    private int status;

    public Status getStatus() {
        return Status.getByOrdinal(status);
    }

}
  • 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-08T11:48:21+00:00Added an answer on June 8, 2026 at 11:48 am

    Perhaps by iterating through the types:

    public enum Status {
        OK(0), PENDING(1), ERROR(2);
    
        private int status;
    
        Status(int status) {
            this.status = status;
        }
    
        public static Status getByOrdinal(int ordinal) {
            for (final Status element : EnumSet.allOf(Status.class)) {
                if (element.ordinal() == ordinal) {
                    return element;
                }
            }
    
            throw new IllegalArgumentException("Unknown status type: " + ordinal);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document that contains a series of item nodes that look
I have a simple class that contains a hashmap: @XmlRootElement() public class Customer {
I have an xml string below that I've turned into an object using $content
I have a function that outputs an XML string: <expensesAC> <cashflow> <month>6</month> <cash>300</cash> <projected>null</projected>
I have a task of parsing a simple XML-formatted string in a MySQL stored
I have a simple doc.xml file which contains a single root element with a
<input src=LOGO.JPG type=image name=imagem> I have an xml element that contains the image path
I have a WIX XML document that contains 2,000+ file tags. I am trying
I have an xml document that contains some html. <begin-line> <verse-num>6</verse-num>a mixed people<footnote id=f2>
I have an XML configuration file that contains a list of documents with field

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.