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

The Archive Base Latest Questions

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

I have to generate the following xml in my program using JAXB. <?xml version=1.0

  • 0

I have to generate the following xml in my program using JAXB.

         <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <Employee empId="12345">
          <name>ABC</name>
          <address type="Residence">Bangalore</address>
         </Employee>

I have to generate the above xml using JAXB.
I am having the Employee class as follows:

Employee Class

    package mypack;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement (name = "Employee")
    public class Employee {
            private String name;
        private String address;
        private int empId;
        private String addressType;

        @XmlAttribute
        public int getEmpId() {
            return empId;
        }
        public void setEmpId(int empId) {
            this.empId = empId;
        }

        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }

        public String getType() {
            return addressType;
        }
        public void setType(String addressType) {
            this.addressType = addressType;
        }   
    }

I am using JAXB to marshal the object.

Employee emp = new Employee();
            emp.setName("ABC");
            emp.setEmpId(12345);
                emp.setAddress("Bangalore");
                    emp.setType("Residence");
                    JAXBContext context = JAXBContext.newInstance(Employee.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.marshal(emp, System.out);

It does not generate the required xml. Rather it generates the xml as follows:

**Xml Being Generated**

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Employee empId="12345">
       <name>ABC</name>
       <address>Bangalore</address>
       <type>Residence</type>
    </Employee>

Actually I don’t know how to annotate the type attribute so that I will generate the xml as <address type="Resident">Bangalore</address>
Is my Employee class should be like as above? If yes, then how to annotate the type attribute so that it will come as an attribute in <address> tag.

Please help me out.

  • 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-26T08:22:14+00:00Added an answer on May 26, 2026 at 8:22 am

    Note: I’m the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.

    You could use the MOXy’s @XmlPath extension for this use case:

    @XmlPath("address/@type")
    public String getType() {
        return addressType;
    }
    

    Employee

    package mypack;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name = "Employee")
    public class Employee {
    
        private String name;
        private String address;
        private int empId;
        private String addressType;
    
        @XmlAttribute
        public int getEmpId() {
            return empId;
        }
    
        public void setEmpId(int empId) {
            this.empId = empId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @XmlPath("address/@type")
        public String getType() {
            return addressType;
        }
    
        public void setType(String addressType) {
            this.addressType = addressType;
        }
    }
    

    Demo

    package mypack;
    
    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(Employee.class);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/mypack/input.xml");
            Employee employee = (Employee) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(employee, System.out);
        }
    
    }
    

    Input/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <Employee empId="12345">
       <address type="Resident">Bangalore</address>
       <name>ABC</name>
    </Employee>
    

    For More Information

    • http://blog.bdoughan.com/2010/09/xpath-based-mapping-geocode-example.html
    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

    UPDATE

    If you do not want to use any vendor specific extension then you could introduce a second class to represent the address information:

    Address

    package mypack;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlValue;
    
    public class Address {
    
        private String address;
        private String addressType;
    
        @XmlValue   
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    
        @XmlAttribute
        public String getType() {
            return addressType;
        }
    
        public void setType(String addressType) {
            this.addressType = addressType;
        }
    }
    

    Employee

    package mypack;
    
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    
    @XmlRootElement(name = "Employee")
    public class Employee {
    
        private String name;
        private Address address;
        private int empId;
    
        @XmlAttribute
        public int getEmpId() {
            return empId;
        }
    
        public void setEmpId(int empId) {
            this.empId = empId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = address;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wrestling to deserialize the following XML: <?xml version=1.0 encoding=utf-8 ?> <conf name=settings> <item
I have an XML file which looks like this : <?xml version=1.0 encoding=UTF-8?> <resultset
I was using the following in web.xml to configure a servlet to dynamically generate
I have the following XML from which I am trying to generate HTML files.
We have configured iReport to generate the following graph: The real data points are
I have the following method (used to generate friendly error messages in unit tests):
I have the following code to take an image and generate the thumbnail. how
I have the following problem: I need to use XSLFO to generate a 2-column
I have the following requirement: Based on some user input, I need to generate
I need to generate the following XML with SOAP: ... <InternationalShippingServiceOption> <ShippingService>StandardInternational</ShippingService> <ShippingServiceCost currencyID=USD>39.99</ShippingServiceCost>

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.