XML : XML parsing issue with jaxb

I am new to jaxb need some help on it.

I am getting null values as out put when trying to execute the below code.could some one please guide me how to achieve the expected out put.

Actual XMl I am trying to unmarsh

  <?xml version="1.0" encoding="UTF-8"?>      <customer >      <cash xmlns:cash="uri:cash" >             <cash:no>10</cash:no>          <cash:name>naveen</cash:name>          <cash:age>27</cash:age>          <cash:phno>9176927613</cash:phno>         </cash>  </customer>    

mapping classes I am using :::

  import javax.xml.bind.annotation.XmlAccessType;  import javax.xml.bind.annotation.XmlAccessorType;  import javax.xml.bind.annotation.XmlElement;  @XmlRootElement(name="cash")  public class Cash {      private int no;      private String name;      private int age;      private Long phno;      public int getNo() {          return no;      }      @XmlElement(name="cash:no",nillable=true)      public void setNo(int no) {          this.no = no;      }      public String getName() {          return name;      }      @XmlElement(name="cash:name",nillable=true)      public void setName(String name) {          this.name = name;      }      public int getAge() {          return age;      }      @XmlElement(name="cash:age",nillable=true)      public void setAge(int age) {          this.age = age;      }      public Long getPhno() {          return phno;      }      @XmlElement(name="cash:phno",nillable=true)      public void setPhno(Long phno) {          this.phno = phno;      }      @Override      public String toString() {          return "Cash [no=" + no + ", name=" + name + ", age=" + age + ", phno="                  + phno + "]";      }  }    

Customer dto:

  import java.util.List;  import javax.xml.bind.annotation.XmlAccessType;  import javax.xml.bind.annotation.XmlAccessorType;  import javax.xml.bind.annotation.XmlElement;  import javax.xml.bind.annotation.XmlRootElement;        @XmlRootElement(name="customer")        public class Customer {            private List<Cash> cash;            public List<Cash> getCash() {              return cash;          }          @XmlElement(name="cash")          public void setCash(List<Cash> cash) {              this.cash = cash;          }              @Override          public String toString() {              return "Customer [cash=" + cash +  "]";          }      }    Marshaling bock:        File  file = new File("cust.xml");          JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();          Customer que= (Customer) jaxbUnmarshaller.unmarshal(file);        System.out.println(que);    

Actual

l output:Customer [cash=[Cash [no=0, name=null, age=0, phno=null]]]

Expected output is the actual XML data.

No comments:

Post a Comment