Raji
Raji

Reputation: 23

Java List into XML Marshalling using JAXB is not working?

Before posting this question, I have done a lots of research on stack-exchange answers for similar type of questions. Also I have tried many solutions to resolve the problem from Google. Finally I think I can ask the problem here:

  1. I am trying to convert a Java List of Account objects into XML using JAXB marshalling process. But the expected is the below first 15 lines of proper xml and currently generated one is last 2 lines of code:

Here it is:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <accounts>
    <account>
      <accountName>Media Team </accountName>
      <ownerName>Support Team</ownerName>
      <ownerEmail>[email protected]</ownerEmail>
      <ownerId>1221323</ownerId>
    <account>
    <account>
       <accountName>Delivery</accountName>
       <ownerName>jadajsnfd</ownerName>
       <ownerEmail>[email protected]</ownerEmail>
       <ownerId>82487282</ownerId>
   </account>
 </accounts>

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 </accounts>

Here is the Accounts class:

@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;
 }
}

Here is the Account class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "account")
public class Account {
    String accountName;
    String ownerName;
    String ownerEmail;
    String ownerId;
//All setters and getters are here public
}

Finally Here is my code:

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();
}

I have tried solution like adding @XMLSeeAlso Annotation and others too. But not working. Please help me resolving this.

Thanks in Advance, Raji

Upvotes: 1

Views: 380

Answers (1)

Smutje
Smutje

Reputation: 18143

In the constructor of Accounts you don't use the list of Account:

public Accounts(List<Account> accounts) {
}

so, change it to

public Accounts(List<Account> accounts) {
    this.accounts = accounts;
}

Upvotes: 1

Related Questions