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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:32:26+00:00 2026-06-09T20:32:26+00:00

I have issue about convert XML to Java Object , in here i use

  • 0

I have issue about convert XML to Java Object , in here i use JAXB.
in my case:

XML DATA :

<persons>
  <person>
     <name>Aris Tonar</name>
     <age />
     <nickname />
     <saldo />
  </person>
</persons>

JAVA CLASS OF XML DATA :

public class Person{
   private String name;
   private Integer age;
   private String nickName;
   private Integer id;
   private Double saldo;

   /**
   *Setter and Getter
   */

}

and when i using standard JAXBContext Unmarshaller code for parse it to Java Object. with code :

JAXBContext context = JAXBContext.newInstance(Person.class.getPackage().getName());
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement element = (JAXBElement) unmarshaller.unmarshal(new StreamSource(new StringReader(stringOfXmlData.toString())));
Person obj = (Person) element.getValue();

it will give result :
Person name = “Aris Tonar”
Person age = null
Person nickname = “”
Person id = null
Person saldo = null

but not that result what i want, i want JAXB will give result :

Person name = “Aris Tonar”
Person age = 0
Person nickname = “”
Person id = null
Person saldo = 0

  1. i want every element which declared in XML DATA will : if it’s value specified jaxb will interpreting it as normal(like name element) but if it’s value not specified jaxb will interpreting it as default value(like age,nickname, saldo elements)
  2. and if element not declared in XML Data, so jaxb will interpreting it as null.(like id element)

whether possible i do it? may be with customizing JAXBContext Code ??

  • 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-09T20:32:28+00:00Added an answer on June 9, 2026 at 8:32 pm
    @XmlType(name = "person", propOrder = {"name", "age", "nickName" , "saldo"})
    public class Person {
    
    private String name;
    private Integer age;
    private String nickName;
    private Integer id;
    private Double saldo;
    
    public Person(String name, Integer age, String nickName, Integer id, Double saldo) {
      this.name = name;
      this.age = age;
      this.nickName = nickName;
      this.id = id;
      this.saldo = saldo;
    }
    
    public Person() {
      //jaxb constructor
    }
    
    @XmlElement(defaultValue="0")
    public Integer getAge() {
      return age;
    }
    
    @XmlTransient
    public Integer getId() {
      return id;
    }
    
    @XmlElement(defaultValue="")
    public String getName() {
      return name;
    }
    
    @XmlElement(name="nickname" , defaultValue="")
    public String getNickName() {
     return nickName;
    }
    
    @XmlElement(defaultValue="0")
    public Double getSaldo() {
      return saldo;
    }
    
    public void setAge(Integer age) {
      this.age = age;
    }
    
    public void setId(Integer id) {
      this.id = id;
    }
    
    public void setName(String name) {
      this.name = name;
    }
    
    public void setNickName(String nickName) {
      this.nickName = nickName;
    }
    
    public void setSaldo(Double saldo) {
      this.saldo = saldo;
    }
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an application an have an issue about running shell command
Read about the issue in this stackoverflow question . Still have the same issue
I have research about an issue and could not find a solution for it
I have read a lot about this issue but mine still seems to be
Please help me about this issue... In my application i have calender where user
I have a serious issue with Liferay 6.1 themes , I know well about
I have a list of Person objects. I want to convert to a Dictionary
I have an issue about type compatibility - check this example: GLbyte vShaderStr[] =
I have searched through numerous questions about how to convert VBS to C# and
I have a question about Java Generics and Collections. It's considered good practice to

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.