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

  • Home
  • SEARCH
  • 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 9055449
In Process

The Archive Base Latest Questions

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

I have a class, that is something like this: public class Property { private

  • 0

I have a class, that is something like this:

public class Property {

    private double floorArea;

    public double getFloorArea() {
        return floorArea;
    }

    @XmlElement
    public void setFloorArea(double floorArea) {
        this.floorArea = floorArea;
    }

}

Which will give me something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<property>
    <floorArea>x</floorArea>
</property>

But I need something like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<property>
    <floorArea>
        <value>x</value>
    </floorArea>
</property>

The API I am using requires it this way. My limited JAXB knowledge is preventing me from figuring this out. Any help is appreciated.

EDIT:

something I am researching. Would I need to create a value class with its own JAXB annotations for this to work? (and set floorArea to the type of value)?

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

    Below is how your use case could be supported with an XmlAdapter using any JAXB (JSR-222) implementation.

    XmlAdapter (DoubleValueAdapter)

    An XmlAdapter is a mechanism that allows an object to be converted to another type of object . Then it is the converted object that is converted to/from XML.

    package forum14045961;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class DoubleValueAdapter extends XmlAdapter<DoubleValueAdapter.AdaptedDoubleValue, Double>{
    
        public static class AdaptedDoubleValue {
            public double value;
        }
    
        @Override
        public AdaptedDoubleValue marshal(Double value) throws Exception {
            AdaptedDoubleValue adaptedDoubleValue = new AdaptedDoubleValue();
            adaptedDoubleValue.value = value;
            return adaptedDoubleValue;
        }
    
        @Override
        public Double unmarshal(AdaptedDoubleValue adaptedDoubleValue) throws Exception {
            return adaptedDoubleValue.value;
        }
    
    }
    

    Property

    The @XmlJavaTypeAdapter annotation is used to specify the XmlAdapter. I needed to change double to Double so I moved the mapping to the field as to not affect the public API.

    package forum14045961;
    
    import javax.xml.bind.annotation.*;
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    
    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Property {
    
        @XmlJavaTypeAdapter(DoubleValueAdapter.class)
        private Double floorArea;
    
        public double getFloorArea() {
            return floorArea;
        }
    
        public void setFloorArea(double floorArea) {
            this.floorArea = floorArea;
        }
    
    }
    

    Demo

    Below is some demo code to prove that everything works.

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

    input.xml/Output

    Below is the input to and output from the demo code.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <property>
        <floorArea>
            <value>1.23</value>
        </floorArea>
    </property>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that looks something like this: public partial class It {
I have a model that looks something like this: public abstract class Parent {
I currently have service classes that look something like this public class UserService :
Good Day, I have a Hibernate mapping that looks something like this: public class
I have a class that looks something like this: public class MyClass { string
I have a templated container class that looks something like this: template <class ItemType>
I have a model that is something like this: class Input(models.Model): details = models.CharField(max_length=1000)
I have three models that look something like this: class Bucket < ActiveRecord::Base has_many
I have a Hibernate mapping that looks something like this: <class name=MyEntity> <set name=scalarSet
We have a class a class that looks something like the following: public class

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.