Ich bin neu zu Jaxb brauche etwas Hilfe.XML-Parsing-Problem mit Jaxb
Ich bekomme Null-Werte als out Put beim Ausführen der unten Code.could einige bitte führen Sie mich, wie Sie die erwartete out Put erreichen.
Actual XMl ich entordnen versuche:
<?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-Klassen Ich verwende:
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 + "]";
}
}
Kunden 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 + "]";
}
}
Rangierung Block:
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);
tatsächliche Ausgang: Customer [cash=[Cash [no=0, name=null, age=0, phno=null]]]
Erwartete Ausgabe ist die tatsächlichen XML-Daten.
Unten ist der Link zu einem Tutorial, das das gleiche tut. Ich bin nicht in der Nähe meines Computers, um zu sehen, was falsch ist. Bitte vergleichen Sie Ihren Code und Sie sollten das Problem eingrenzen. https://www.javacodegeeks.com/2013/02/jaxb-tutorial-getting-started.html – LearningPhase