2010-12-30 4 views
2

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> 

+0

어떤 아이디어를 --added? 나는 아직도 이것에 관해서 일하고있다. ... –

+0

당신이 보여줄 수있는 어떤 코드? –

+0

@Dave Jarvis : 원한다면 XML에서 한 객체를 묶는 내 코드를 보여줄 수 있습니까? –

답변

1

난 당신이 모든 혜택 요소를 포함 행사라는 루트 요소를 작성하는 것이 좋습니다. 이렇게하면 Offers 요소가 비 정렬되고 Offer 요소 목록을 검색 할 수있는 방법이 제공됩니다.

는 example--

<xsd:complexType name="offers"> 
    <xsd:sequence> 
     <xsd:element name="offersList" type="Offers" minOccurs="0" maxOccurs="unbounded"/> 
    </xsd:sequence> 
</xsd:complexType> 
+0

감사합니다. XSD 스키마를 기반으로합니다 : –

+0

루트 요소는 XSD 스키마에서 어디에 있습니까? 나는 내 게시물에 스키마를 맞출 수 없다. –

+0

마침내 게시물에 내 XSD가있다. Lol. 아. 루트 요소가 어디서/어떻게 보일 것인가를 나타내는만큼 친절한가? 나는 너무 혼란 스럽다. 나는 지금 어떤 방향이 필요하다 .. –

관련 문제