PDog
PDog

Reputation: 76

How do I write a deep XML document using JAXB?

I am trying to create an XML document that has a more complex structure than other examples I've seen. Here's my attempt, using three classes:

Band class code:

@XmlRootElement(name = "Band")
@XmlType(propOrder = { "x" })
public class Band {
    private int x;

    public Band() {
        x = 0;
    }

    public int getX() {
       return x;
    }

    public void setX(int x) {
        this.x = x;
    }
}

Program class code:

@XmlRootElement(name = "Program")
@XmlType(propOrder = { "numBands" , "programName", "bandList"})
public class Program {

   private int numBands;
   private String programName;
   private ArrayList<Band> bandlist;

   public void Program() {
      Band band1 = new Band();
      band1.setX(14);
      bandlist.add(band1);

      Band band2 = new Band();
      band2.setX(24);
       bandlist.add(band2);
   }

   public void setNumBands(int nb) {
       numBands = nb;
   }

   @XmlElement(name = "numBands")
   public int getNumBands() {
       return numBands;
   }

   public void setProgramName(String pn) {
       programName = pn;
   }

   @XmlElement(name = "programName")
   public String getProgramName() {
        return programName;
   }

   public void setBandList(ArrayList<Band> bl) {
       bandlist = bl;
   }

   @XmlElementWrapper(name = "bandlist")
   public ArrayList<Band> getBandList() {
       return bandlist;
   }
}

ProgramList class code:

@XmlRootElement()
@XmlType(propOrder = {"programList"})
public class ProgramList {

  private ArrayList<Program> programlist;
  public void ProgramList()   {
     Program program1 = new Program();
     program1.setNumBands(2);
     program1.setProgramName("Program 1");
     programlist.add(program1);

     Program program2 = new Program();
     program2.setNumBands(2);
     program2.setProgramName("Program 2");
     programlist.add(program2);        
  }

  public void setProgramList(ArrayList<Program> programlist) {
      this.programlist = programlist;
  }

  @XmlElementWrapper(name = "programlist")
  public ArrayList<Program> getProgramList() {
      return programlist;
  }
}

Marshalling code: (part of another class that tries to write it)

private void writeXML() throws JAXBException, IOException  {
    ProgramList pl = new ProgramList();
    
    JAXBContext context = JAXBContext.newInstance(ProgramList.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(pl, new File("program.xml"));
}

It all compiles OK, and runs without exceptions. But the XML document looks like this:

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

Not what I expected. I expected that the instantiatioin of a ProgramList object would have created two programs with two bands each and for the whole thing to end up in the XML file.

Am I doing this all wrong?

I also tried this line:

JAXBContext context = JAXBContext.newInstance(new Class[] {ProgramList.class, Program.class, Band.class});

Same result.

Upvotes: 1

Views: 579

Answers (1)

bdoughan
bdoughan

Reputation: 148977

The following is not a constructor since it has the void return type. It is just a method with the same name as a constructor. This means this logic isn't running to populate your object model.

public void ProgramList()   {
     Program program1 = new Program();
     program1.setNumBands(2);
     program1.setProgramName("Program 1");
     programlist.add(program1);

     Program program2 = new Program();
     program2.setNumBands(2);
     program2.setProgramName("Program 2");
     programlist.add(program2);        
  }

It should be:

public ProgramList()   {
     programlist = new ArrayList<Program>(2);

     Program program1 = new Program();
     program1.setNumBands(2);
     program1.setProgramName("Program 1");
     programlist.add(program1);

     Program program2 = new Program();
     program2.setNumBands(2);
     program2.setProgramName("Program 2");
     programlist.add(program2);        
}

Note: Since JAXB is configuration by exception you are adding a lot of annotations that aren't necessary. The following should help:

Upvotes: 1

Related Questions