1 객체를 문제없이 마샬링하고 비 정렬화할 수 있습니다 (netbeans에서). 여러 객체로이 작업을 수행하는 방법을 알아야합니까? XML에서 배열로 3 개의 객체를 비 정렬 화하려고 할 때 null 포인터 예외를 생성 할 수 없습니다. 그래서 나는 3을 제대로 마샬링했는지조차 알지 못합니다. 개체를 선언 한 다음 jaxbu 또는 jaxbm 명령을 사용하는 기본 개념은 알고 있지만 둘 이상의 개체에서 작동하는 것을보고 싶습니다.JAXB 웹 서비스 : 다중 객체 마샬링
** TLDR : 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());
} 캐치 (JAXBException에 전자) {에서 System.out.println ("오류"+ E) : 감사
코드는 그 보안관 XML에서 하나의 개체가 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
}
}
,451,515,
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>
어떤 아이디어를 --added? 나는 아직도 이것에 관해서 일하고있다. ... –
당신이 보여줄 수있는 어떤 코드? –
@Dave Jarvis : 원한다면 XML에서 한 객체를 묶는 내 코드를 보여줄 수 있습니까? –