我可以毫无问题地编组和解组 1 个对象(在 netbeans 中);我需要知道如何处理多个对象?尝试将 3 个对象从 XML 解组为数组时,除了空指针异常之外,我什么也不能生成;所以我什至不知道我是否正确地编组了 3 个。我知道声明对象,然后使用 jaxbu 或 jaxbm 命令的基本思想,但我希望看到这适用于多个对象。
**TLDR:如何将单个类的多个对象编组/解组到 XML 中?谢谢
我有从 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("错误 " + e);}
好的,所以 Xml 是:
<?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
}
}
XSD 文件:
<?xml version="1.0" encoding="UTF-8"?><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://simple.example.com/CInfoXmlDoc"
xmlns="http://simple.example.com/CInfoXmlDoc">
<xsd:element name="offer">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Origin" type="xsd:string"/>
<xsd:element name="Destination" type="xsd:string"/>
<xsd:element name="Airline" type="xsd:string"/>
<xsd:element name="Proximity" type="xsd:int"/>
<xsd:element name="Seats" type="xsd:int"/>
<xsd:element name="Date" type="xsd:date"/>
<xsd:element name="Fare">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Currency" type="xsd:string"/>
<xsd:element name="Value" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:attribute></xsd:attribute>
</xsd:sequence>
</xsd:complexType>
</xsd:element>