Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9137249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:04:44+00:00 2026-06-17T09:04:44+00:00

I am trying to create an XML document that has a more complex structure

  • 0

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:

  • a Band class, identifying a vertical band of color on an image. For now, it only has an X value, where it is on the image.
  • a Program class, "program" meaning a template of a certain kind of image. It has the number of bands on the image, a name for the program, and a list of Band objects.
  • a ProgramList class, containing a list of Programs.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:04:45+00:00Added an answer on June 17, 2026 at 9:04 am

    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:

    • http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an XML document in Java that contains the following Element:
I'm trying to create an in-memory xml document such that the root's child nodes
I am trying to create an XSLT to transform an XML document and having
I'm trying to create an xml entry that looks like this using python and
I'm trying to create an XML sitemap using CakePHP, from a table which has
I'm trying to create a large XML tree in R. Here's a simplified version
I'm trying to recreate a SyndicationFeed object (System.ServiceModel.Syndication) from XML data that has been
I have an XML document that has a collection of objects. Each object has
I am new to java and I am trying to create an XML document
I'm trying to build a rather complex XML document. I have a bunch of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.