title: JAXB Web Services: Multiple Object Marshalling
body: <p>I can marshal and unmarshal 1 object with no problems (in netbeans); I need to know how to do this with multiple objects? I can't generate anything but null pointer exceptions when trying to unmarshal 3 objects into an array from XML; so I don't even know if I've marshalled the 3 out correctly. I know the basic idea of declaring the object, then using the jaxbu or jaxbm command, but I'd like to see this working for more than one object.</p>
  
  <p>**TLDR: How do I marshal/unmarshal multiple objects of a single class into/out of XML?? THANKS</p>
  
  <p>Code I have that marshals one object from XML:</p>
  
  <pre><code>try {
  JAXBContext jc = JAXBContext.newInstance ("myOffers");
  Unmarshaller u = jc.createUnmarshaller ();
  myOffers.Offer flight = (myOffers.Offer) u.unmarshal( new FileInputStream( "offers.xml" ));
  
  System.out.println ("Airline is  : " + flight.getAirline());
  System.out.println ("Origin is : " + flight.getOrigin());
  System.out.println ("Destination is : " + flight.getDestination());
  System.out.println ("Seats available : " + flight.getSeats());
  System.out.println("Proximity to City Centre is : " + flight.getProximity());
  System.out.println("Currency : " + flight.fare.getCurrency());
  System.out.println("Value : " + flight.fare.getValue());
  </code></pre>
  
  <p>} catch (JAXBException e) {    System.out.println("Error " + e);}</p>
  
  <p>ok so the Xml is:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns2:offer xmlns:ns2="http://simple.example.com/CInfoXmlDoc"&gt;
      &lt;Origin&gt;Nottingham&lt;/Origin&gt;
      &lt;Destination&gt;Istanbul&lt;/Destination&gt;
      &lt;Airline&gt;Lufthansa&lt;/Airline&gt;
      &lt;Proximity&gt;10&lt;/Proximity&gt;
      &lt;Seats&gt;260&lt;/Seats&gt;
      &lt;Fare&gt;
          &lt;Currency&gt;GBP&lt;/Currency&gt;
          &lt;Value&gt;300&lt;/Value&gt;
      &lt;/Fare&gt;
  &lt;/ns2:offer&gt;
  &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns2:offer xmlns:ns2="http://simple.example.com/CInfoXmlDoc"&gt;
      &lt;Origin&gt;Birmingham&lt;/Origin&gt;
      &lt;Destination&gt;Cairo&lt;/Destination&gt;
      &lt;Airline&gt;Monarch&lt;/Airline&gt;
      &lt;Proximity&gt;15&lt;/Proximity&gt;
      &lt;Seats&gt;350&lt;/Seats&gt;
      &lt;Fare&gt;
          &lt;Currency&gt;GBP&lt;/Currency&gt;
          &lt;Value&gt;300&lt;/Value&gt;
      &lt;/Fare&gt;
  &lt;/ns2:offer&gt;
  </code></pre>
  
  <p>Which was generated by my marshal code found here:</p>
  
  <pre><code>public static void main(String[] args) throws FileNotFoundException {
  int i = 0;
  int arraySize = 2;
  
  myOffers.Offer offer[] = new myOffers.Offer[arraySize];
  offer[i] = new myOffers.Offer();
  offer[i].fare = new myOffers.Offer.Fare();
  
  offer[i].setAirline("Lufthansa");
  offer[i].setOrigin("Nottingham");
  offer[i].setDestination("Istanbul");
  offer[i].setSeats(260);
  offer[i].setProximity(10);
  offer[i].fare.currency = "GBP";
  offer[i].fare.value = 300;
  
  i++;
  offer[i] = new myOffers.Offer();
  offer[i].fare = new myOffers.Offer.Fare();
  offer[i].setAirline("Monarch");
  offer[i].setOrigin("Birmingham");
  offer[i].setDestination("Cairo");
  offer[i].setSeats(350);
  offer[i].setProximity(15);
  offer[i].fare.currency = "GBP";
  offer[i].fare.value = 300;
  
  
  try {
  int n = 0;
  FileOutputStream f = new FileOutputStream("offers.xml");
  javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(offer[n].getClass().getPackage().getName());
  javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
  marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
  marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  
  
  while (n &lt; arraySize)
  { 
  marshaller.marshal(offer[n], f);
  n++;
  }
  } catch (javax.xml.bind.JAXBException ex) {
  // XXXTODO Handle exception
  java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
  }
  }
  </code></pre>
  
  <p>XSD FILE :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          targetNamespace="http://simple.example.com/CInfoXmlDoc"
          xmlns="http://simple.example.com/CInfoXmlDoc"&gt;                
  &lt;xsd:element name="offer"&gt;
      &lt;xsd:complexType&gt;
          &lt;xsd:sequence&gt;
              &lt;xsd:element name="Origin" type="xsd:string"/&gt;
              &lt;xsd:element name="Destination" type="xsd:string"/&gt;
              &lt;xsd:element name="Airline" type="xsd:string"/&gt;
              &lt;xsd:element name="Proximity" type="xsd:int"/&gt;
              &lt;xsd:element name="Seats" type="xsd:int"/&gt;
              &lt;xsd:element name="Date" type="xsd:date"/&gt;
              &lt;xsd:element name="Fare"&gt;
                  &lt;xsd:complexType&gt;
                      &lt;xsd:sequence&gt;
                          &lt;xsd:element name="Currency" type="xsd:string"/&gt;
                          &lt;xsd:element name="Value" type="xsd:int"/&gt;
                      &lt;/xsd:sequence&gt;
                  &lt;/xsd:complexType&gt;
               &lt;/xsd:element&gt;
               &lt;xsd:attribute&gt;&lt;/xsd:attribute&gt;
           &lt;/xsd:sequence&gt;             
      &lt;/xsd:complexType&gt;
  &lt;/xsd:element&gt;
  </code></pre>
  
  <p></p>