I have created an XML schema that holds flights and i want to use to be able to create an XML document that contains all the flights. However, i can only write single flights to the XML file. So can anyone tell me how i can add multiple flights to the XML file.
Here is the XML schema:
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/Flights"
xmlns:tns="http://xml.netbeans.org/schema/Flights"
elementFormDefault="qualified">
<xsd:element name="Flight">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="OriginCity" type="xsd:string"/>
<xsd:element name="DestinationCity" type="xsd:string"/>
<xsd:element name="Airline" type="xsd:string"/>
<xsd:element name="AvailableSeats" type="xsd:int"/>
<xsd:element name="Currency" type="xsd:string"/>
<xsd:element name="Value" type="xsd:double"/>
</xsd:sequence>
<xsd:attribute name="ID" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>
My code that i tried:
public class Cwtestings {
public static void main(String[] args) {
// TODO code application logic here
FlightRequestss.Flight quickXML = new FlightRequestss.Flight();
quickXML.setID(1);
quickXML.setOriginCity("London");
quickXML.setDestinationCity("Glasgow");
quickXML.setAirline("BMI");
quickXML.setAvailableSeats(200);
quickXML.setCurrency("GBP");
quickXML.setValue(150.00);
quickXML.setID(2);
quickXML.setOriginCity("Nottingham");
quickXML.setDestinationCity("Paris");
quickXML.setAirline("EasyJet");
quickXML.setAvailableSeats(400);
quickXML.setCurrency("GBP");
quickXML.setValue(155.00);
quickXML.setID(3);
quickXML.setOriginCity("Paris");
quickXML.setDestinationCity("Nottingham");
quickXML.setAirline("EasyJet");
quickXML.setAvailableSeats(300);
quickXML.setCurrency("EUR");
quickXML.setValue(156.00);
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.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);
marshaller.marshal(quickXML, System.out);
} catch (javax.xml.bind.JAXBException ex) {
// XXXTODO Handle exception
java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
}
}
}
The above code will only display flight witht he ID 3 as it was the last thing enetered so i assume its overwriting each time.
After writing all the flights to a file i then need to be able to open that file and search for certain things. If possible, could i read them into a List somehow?