[英]Java List into XML Marshalling using JAXB is not working?
在發布此問題之前,我對類似類型問題的堆棧交換答案進行了大量研究。 我也嘗試了許多解決方案來解決Google的問題。 最后我想我可以在這里提出問題:
這里是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<accounts>
<account>
<accountName>Media Team </accountName>
<ownerName>Support Team</ownerName>
<ownerEmail>sbjba@gmail.com</ownerEmail>
<ownerId>1221323</ownerId>
<account>
<account>
<accountName>Delivery</accountName>
<ownerName>jadajsnfd</ownerName>
<ownerEmail>bfjaja@gmail.com</ownerEmail>
<ownerId>82487282</ownerId>
</account>
</accounts>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
</accounts>
這是Accounts類:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "accounts")
public class Accounts {
@XmlElement(name = "account", type = Account.class)
private List<Account> accounts = new ArrayList<Account>();
public Accounts(){
}
public Accounts(List<Account> accounts) {
}
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
}
這是Account類:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "account")
public class Account {
String accountName;
String ownerName;
String ownerEmail;
String ownerId;
//All setters and getters are here public
}
最后這是我的代碼:
File videosFile = new File("C:/AccountInfo.xml");
List<Account> accounts = null;
List<KalturaMediaEntry> mediaList = null;
mediaList = getAllLatestMedia();
if (mediaList.size() >= 1) {
System.out.println("mediaList.size() ---------->"
+ mediaList.size());
accounts = setMediaDataByAccount(mediaList);
if (accounts.size() >= 1) {
System.out.println("videos.size() --------->"
+ accounts.size());
marshalVideos(accounts, videosFile);
}
}
public static void marshalVideos(List<Account> accounts,
File videosFile) throws JAXBException, IOException {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(videosFile, true), "UTF-8"));
final Marshaller m = JAXBContext.newInstance(Accounts.class)
.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(new Accounts(accounts), writer);
writer.close();
}
我也嘗試過添加@XMLSeeAlso
Annotation等解決方案。 但是沒有用。 請幫我解決這個問題。
在此先感謝,拉吉
在Accounts
的構造函數中,您不使用Account
列表:
public Accounts(List<Account> accounts) {
}
因此,將其更改為
public Accounts(List<Account> accounts) {
this.accounts = accounts;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.