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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:37:12+00:00 2026-06-11T17:37:12+00:00

I want to Create an XML using JAXB. It should have attributes in it

  • 0

I want to Create an XML using JAXB.
It should have attributes in it whose values have to come from a property defined in corresponding class.

The current output is giving a property as a child tag, and not as an attribute in the main tag.

The Structure of my desired XML is as follows.

<?xml version="1.0" encoding="UTF-8"?>
<company>
    <department name="hr">
        <branch name="hr-recruitment">
            <manager> Manasa </manager>
            <phone>992222222</phone>
            <salary> 20000 </salary>
        </branch>
        <branch name="hr-finance">
            <manager> Sunder </manager>
            <phone>993333332</phone>
            <salary> 50000 </salary>
        </branch>
    </department>
    <department name="transport">
        <branch name="transport-employee">
            <manager> Raman </manager>
            <phone>888888888</phone>
            <salary> 30000 </salary>
        </branch>
        <branch name="transport-goods">
            <manager> Sheela </manager>
            <phone>99999999</phone>
            <salary> 75000 </salary>
        </branch>
    </department>
</company> 

Here are the 3 classes marked with the JAXB annotations:

1) company.class

@XmlRootElement(name = "tarang")
public class Company {

    private List<Department> listDepartments;

    public Company(List<Department> listDepartments) {
        this.listDepartments = listDepartments;
    }

    public Company() {

    }

    @XmlElementRef
    public List<Department> getListDepartments() {
        return listDepartments;
    }

    public void setListDepartments(ArrayList<Department> listDepartments) {
        this.listDepartments = listDepartments;
    }

}

2) Department.class

@XmlRootElement(name = "department")
public class Department {

    private String strDepartmentName;

    private List<Branch> listBranchs;

    public Department(String strDepartmentName, List<Branch> listBranchs) {
        this.strDepartmentName = strDepartmentName;
        this.listBranchs = listBranchs;
    }

    public Department() {

    }

    @XmlElement(name = "name")
    public String getStrDepartmentName() {
        return strDepartmentName;
    }

    public void setStrDepartmentName(String strDepartmentName) {
        this.strDepartmentName = strDepartmentName;
    }

    @XmlElementRef
    public List<Branch> getListBranchs() {
        return listBranchs;
    }

    public void setListBranchs(List<Branch> listBranchs) {
        this.listBranchs = listBranchs;
    }

}

3) Branch.class

@XmlRootElement(name = "branch")
public class Branch {

    private String strName;
    private String strManagerName;
    private String strPhone;
    private int intSalary;

    public Branch(String strName, String strManagerName, String strPhone,
            int intSalary) {
        this.strName = strName;
        this.strManagerName = strManagerName;
        this.strPhone = strPhone;
        this.intSalary = intSalary;
    }

    public Branch() {
        // TODO Auto-generated constructor stub
    }

    @XmlElement(name = "name")
    public String getStrName() {
        return strName;
    }

    public void setStrName(String strName) {
        this.strName = strName;
    }

    @XmlElement(name = "manager")
    public String getStrManagerName() {
        return strManagerName;
    }

    public void setStrManagerName(String strManagerName) {
        this.strManagerName = strManagerName;
    }

    @XmlElement(name = "phone")
    public String getStrPhone() {
        return strPhone;
    }

    public void setStrPhone(String strPhone) {
        this.strPhone = strPhone;
    }

    @XmlElement(name = "salary")
    public int getIntSalary() {
        return intSalary;
    }

    public void setIntSalary(int intSalary) {
        this.intSalary = intSalary;
    }

}

My current design contains 3 classes.

1) Company 2) Department 3) Branch

The relations are:

1) class Company -has a- List of Departments.
2) class Department -has a- name (String).
3) class Department -has a- List of Branches.
4) class Branch -has a- name (String)
5) class Branch -has a- manager (String)
6) class Branch -has a- phone (String)
7) class Branch -has a- salary (String)

The current Output is:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <tarang>
        <department>
            <branch>
                <salary>50000</salary>
                <manager>Manasa</manager>
                <name>hr-recruitment</name>
                <phone>999999999</phone>
            </branch>
            <branch>
                <salary>40000</salary>
                <manager>Sundar</manager>
                <name>hr-finance</name>
                <phone>888888888</phone>
            </branch>
            <name>hr</name>
        </department>
        <department>
            <branch>
                <salary>30000</salary>
                <manager>Raman</manager>
                <name>transport-employee</name>
                <phone>7777777777</phone>
            </branch>
            <branch>
                <salary>20000</salary>
                <manager>Sheela</manager>
                <name>transport-goods</name>
                <phone>6666666666</phone>
            </branch>
            <name>tranport</name>
        </department>
    </tarang>

The name of the department, and name of the branch is not getting appended as a attribute.
Please suggest me the corrections in the classes, where the @xmlattribute annotation has to be applied for me to get the desired output.

  • 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-11T17:37:13+00:00Added an answer on June 11, 2026 at 5:37 pm

    @XmlAttribute should be added to the field declaration as follows in your Department class:

    @XmlAttribute(name = "name")
    private String strDepartmentName;
    

    See here for more details about XmlAttribute annotation.

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

Sidebar

Related Questions

I have a stream object, and I want to create and output xml using
I want to create atom xml representations for my REST resources using Restlet. Should
I want to create a ASP.NET user control from an XML using XSLT. Currently
I'm using JPA 2.0 and want to create a unique constraint using XML, not
i'm using the DOMdocument class in php whenever i want to create a XML
I want to create a component by giving XML source input directly using core
I want to create a Button otherthan creating it from xml layout.i want to
I have an XML file (on the left) and I want to create multiple
I want to create an xml file which have '&' in one of its
I am using Linq To Xml to create an Xml file from DataSet. This

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.