@ XmlRootElementアノテーションなしでクラスに対してアンマーシャリングできる方法はありますか?または、注釈を入力する義務はありますか?例えばXmlRootElementアノテーションなしのJAXBアンマーシャリング?
:
public class Customer {
private String name;
private int age;
private int id;
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
@XmlAttribute
public void setId(int id) {
this.id = id;
}
}
し、適切に注釈付きクラスのアンマーシャルのコードは次のようになりましょう:詳細を残し
try {
File file = new File("C:\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Customer customer = (Customer) jaxbUnmarshaller.unmarshal(file);
System.out.println(customer);
} catch (JAXBException e) {
e.printStackTrace();
}
。
、そうでない場合またはXmlRootElement''なし、JAXBはない '除き、他の注釈を必要としませんあなた前述したように – Xstian
を働くん@ XmlAttribute'と '@XmlElement'が出力を記述する – Xstian
アンマーシャリングする前にマーシャリングする必要がありますか? –