JAXB Web Services: Multiple Object Marshalling
Posted
by
Luke Evans
on Stack Overflow
See other posts from Stack Overflow
or by Luke Evans
Published on 2010-12-30T11:13:02Z
Indexed on
2010/12/30
16:53 UTC
Read the original article
Hit count: 291
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.
**TLDR: How do I marshal/unmarshal multiple objects of a single class into/out of XML?? THANKS
Code I have that marshals one object from XML:
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());
} catch (JAXBException e) { System.out.println("Error " + e);}
ok so the Xml is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:offer xmlns:ns2="http://simple.example.com/CInfoXmlDoc">
<Origin>Nottingham</Origin>
<Destination>Istanbul</Destination>
<Airline>Lufthansa</Airline>
<Proximity>10</Proximity>
<Seats>260</Seats>
<Fare>
<Currency>GBP</Currency>
<Value>300</Value>
</Fare>
</ns2:offer>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:offer xmlns:ns2="http://simple.example.com/CInfoXmlDoc">
<Origin>Birmingham</Origin>
<Destination>Cairo</Destination>
<Airline>Monarch</Airline>
<Proximity>15</Proximity>
<Seats>350</Seats>
<Fare>
<Currency>GBP</Currency>
<Value>300</Value>
</Fare>
</ns2:offer>
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 < 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
}
}
Which was generated by my marshal code found here:
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 < 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
}
}
Apologies, I'm fining this editor quite appalling but thats another matter. Whats wrong with [code][/code] tags...
© Stack Overflow or respective owner