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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:03:30+00:00 2026-06-06T01:03:30+00:00

In my XML I have <myelem required=false/> How I can read the required attribute

  • 0

In my XML I have

<myelem required="false"/>

How I can read the required attribute as a boolean? I can read it as String and inside a getter do this: return new Boolean(required)
But maybe there are some more elegant ways?

  • 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-06T01:03:32+00:00Added an answer on June 6, 2026 at 1:03 am

    Just simply use boolean for the member in your Java class:

    @XmlAttribute
    private boolean required;
    

    Or, if you use getter-setter style of mapping:

    @XmlAttribute
    public boolean isRequired() {
        return required;
    }
    

    The JAXB unmarshaller is able to interpret "true" and "false" strings in the XML document as boolean value.

    UPDATE:

    I tested this with the following classes:

    test/MyElem.java:

    package test;
    
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="myelem")
    public class MyElem {
    
        private boolean required;
    
        @XmlAttribute
        public boolean isRequired() {
            return required;
        }
    
        public void setRequired(boolean value) {
            required = value;
        }
    
    }
    

    Test.java:

    import javax.xml.bind.*;
    import java.io.*;
    import test.*;
    
    public class Test {
        public static void main(String[] args) {
            try {
                JAXBContext jc = JAXBContext.newInstance(MyElem.class);
                Unmarshaller u = jc.createUnmarshaller();
                Object o = u.unmarshal( new File( "test.xml" ) );   
                System.out.println(((MyElem)o).isRequired());
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    }
    

    And with the following input (test.xml):

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <myelem required="true"/>
    

    I get the correct result on the console:

    true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have XML-request from web service. It looks like this. So, how can I
I Have XML similar to this(omitted parts for brevity): <uformrecord> <state>Submitted</state> <created>2012-06-19T11:31:54</created> <updated>2012-06-19T11:32:13</updated> <id>53225sas3c1-d727-42cd-93a6-97cd778e5ee9</id>
I have xml like this: <rule> <word>I</word> <word>need</word> <word>more</word> <marker> <word>money</word> </marker> <word>now</word> </rule>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which
I have XML String: <GroupBy Collapse=\TRUE\ GroupLimit=\30\> <FieldRef Name=\Department\ /> </GroupBy> <OrderBy> <FieldRef Name=\Width\
I have xml files been imported onto an FTP server. This is stored in
I have XML in this format - <Areas> <Area> <Property Name=Test11>a1</Property> <Property Name=Test12>a2</Property> <Property
I have xml file in server. I am parsing this xml using DOM(xml is
I have XML files containing data like this... <parent> <boy/> </parent> <parent> <girl/> <parent>
I have XML-file with settings like this <?xml version=1.0 encoding=utf-8 ?> <configuration> <configSections> <sectionGroup

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.