2012-08-22 4 views
1

내 프로젝트에서 JaxB 오브젝트로 xml 파일을 생성했습니다. 이제 다시 언 마샬을 JAXB 개체로 지정합니다. unmarshalling 할 때 classcastException throw합니다.xml 파일을 비동기 해제 할 수 없습니다.

의 I가 작성한 클래스를 찾아주세요 :

public class ReservationTest1 { 

    public static void main(String []args) throws IOException, JAXBException 
    { 

     JAXBContext jaxbContext = JAXBContext.newInstance(com.hyatt.Jaxb.makeReservation.request.OTAHotelResRQ.class); 
     Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); 
     @SuppressWarnings("unchecked") 
     JAXBElement bookingElement = (JAXBElement) unmarshaller.unmarshal(
       new FileInputStream("D://myproject//Reservation.xml")); 


     System.out.println(bookingElement.getValue()); 

    } 
} 

당신이 나에게 그것을 해결하기 위해 유용한 정보를 제공하시기 바랍니다 수 있습니다.

답변

1

비 정렬 화 된 객체가 다음 클래스 대신 JAXBElement의 인스턴스의 인스턴스를 얻을 것이다 @XmlRootElement의 주석을 붙일 수 있고있는 경우 당신은 ClassCastException이에게

을 얻고있는 이유. 당신은 항상 관계없이 도메인 객체 또는 JAXBElement 당신이 JAXBIntrospector 사용할 수있는 비 정렬 화 조작에서 반환 여부의 도메인 객체의 인스턴스를 받으려면

FileInputStream xml = new FileInputStream("D://myproject//Reservation.xml"); 
OTAHotelResRQ booking = (OTAHotelResRQ) unmarshaller.unmarshaller.unmarshal(xml); 

항상 도메인 개체를

를 가져옵니다. 오히려 항상 클래스 매개 변수를 취하는 unmarshal 방법 중 하나를 사용할 수 있습니다 JAXBElement의 인스턴스를받을 경우

FileInputStream xml = new FileInputStream("D://myproject//Reservation.xml"); 
Object result = unmarshaller.unmarshaller.unmarshal(xml); 
OTAHotelResRQ booking = (OTAHotelResRQ) JAXBIntrospector.getValue(result); 

항상 JAXBElement 첨부

를 가져옵니다. 추가 정보

StreamSource xml = new StreamSource("D://myproject//Reservation.xml"); 
JAXBElement<OTAHotelResRQ> bookingElement = 
    unmarshaller.unmarshal(xml, OTAHotelResRQ.class); 

관련 문제