简体   繁体   中英

How to unmarshal XML with multiple namespaces in different xml or ignore namespace in jaxb

I have two XML's and corresponding Java files (created by trang.jar and xjc tools)

XML 1 :

<HSI_ContractDetailModificationRQ xmlns="http://axis.frontend.hsi.hotelbeds.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hotelbeds.com/schemas/2005/06/messages/HSI_ContractDetailModificationRQ.xsd">
<Language>CAS</Language>
<Credentials>
    <User>PRUEBASHSI</User>
    <Password>HSI</Password>
</Credentials>
<Contract>
    <Name>TEST HSI</Name>
    <IncomingOffice code="51"/>
    <Sequence>29769</Sequence>
</Contract>
<MinimumStayList>
    <MinimumStay>
        <DateFrom date="20070401"/>
        <DateTo date="20070630"/>
        <MinNumberOfDays>4</MinNumberOfDays>
        <MaxNumberOfDays>12</MaxNumberOfDays>
    </MinimumStay>
</MinimumStayList>
</HSI_ContractDetailModificationRQ>  

java file created by xjc :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"contract",
"roomList",
"minimumStayList"
})
@XmlRootElement(name = "HSI_ContractDetailModificationRQ")
public class HSIContractDetailModificationRQ
extends CoreRequest
implements Serializable
{

private final static long serialVersionUID = 20070801L;
@XmlElement(name = "Contract", required = true)
protected Contract contract;
@XmlElement(name = "RoomList")
protected ServiceHotelRoomListRQ roomList;
@XmlElement(name = "MinimumStayList")
protected HSIContractDetailModificationRQ.MinimumStayList minimumStayList;

/**
 * Gets the value of the contract property.
 * 
 * @return
 *     possible object is
 *     {@link Contract }
 *     
 */
public Contract getContract() {
    return contract;
}

/**
 * Sets the value of the contract property.
 * 
 * @param value
 *     allowed object is
 *     {@link Contract }
 *     
 */
public void setContract(Contract value) {
    this.contract = value;
}

public boolean isSetContract() {
    return (this.contract!= null);
}

/**
 * Gets the value of the roomList property.
 * 
 * @return
 *     possible object is
 *     {@link ServiceHotelRoomListRQ }
 *     
 */
public ServiceHotelRoomListRQ getRoomList() {
    return roomList;
}

/**
 * Sets the value of the roomList property.
 * 
 * @param value
 *     allowed object is
 *     {@link ServiceHotelRoomListRQ }
 *     
 */
public void setRoomList(ServiceHotelRoomListRQ value) {
    this.roomList = value;
}

public boolean isSetRoomList() {
    return (this.roomList!= null);
}

/**
 * Gets the value of the minimumStayList property.
 * 
 * @return
 *     possible object is
 *     {@link HSIContractDetailModificationRQ.MinimumStayList }
 *     
 */
public HSIContractDetailModificationRQ.MinimumStayList getMinimumStayList() {
    return minimumStayList;
}

/**
 * Sets the value of the minimumStayList property.
 * 
 * @param value
 *     allowed object is
 *     {@link HSIContractDetailModificationRQ.MinimumStayList }
 *     
 */
public void setMinimumStayList(HSIContractDetailModificationRQ.MinimumStayList value) {
    this.minimumStayList = value;
}

public boolean isSetMinimumStayList() {
    return (this.minimumStayList!= null);
}


/**
 * <p>Java class for anonymous complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType>
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="MinimumStay" type="{}MinimumStayModRQ2" maxOccurs="unbounded"/>
 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "minimumStay"
})
public static class MinimumStayList
    implements Serializable
{

    private final static long serialVersionUID = 20070801L;
    @XmlElement(name = "MinimumStay", required = true)
    protected List<MinimumStayModRQ2> minimumStay;

    /**
     * Gets the value of the minimumStay property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the minimumStay property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getMinimumStay().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link MinimumStayModRQ2 }
     * 
     * 
     */
    public List<MinimumStayModRQ2> getMinimumStay() {
        if (minimumStay == null) {
            minimumStay = new ArrayList<MinimumStayModRQ2>();
        }
        return this.minimumStay;
    }

    public boolean isSetMinimumStay() {
        return ((this.minimumStay!= null)&&(!this.minimumStay.isEmpty()));
    }

    public void unsetMinimumStay() {
        this.minimumStay = null;
    }

}

}  

XML 2 :

    <HSI_ContractInventoryModificationRS xmlns="http://www.hotelbeds.com/schemas/2005/06   /messages" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
xsi:schemaLocation="http://www.hotelbeds.com/schemas/2005/06/messages         
/HSI_ContractInventoryModificationRS.xsd" echoToken="echotoken">
<AuditData>
<ProcessTime>438</ProcessTime>
<Timestamp>2007-02-12 16:42:37.242</Timestamp>
<RequestHost>10.0.1.221</RequestHost>
<ServerName>ITINTERNET26</ServerName>
<ServerId>LO</ServerId>
<SchemaRelease>2005/06</SchemaRelease>
<HydraCoreRelease>2.0.200702091006</HydraCoreRelease>
<HydraEnumerationsRelease>1.0.200702091006</HydraEnumerationsRelease>
<MerlinRelease>N/A</MerlinRelease>
</AuditData>
<Status>Y</Status>
</HSI_ContractInventoryModificationRS>   

java file created by xjc :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"status"
})
@XmlRootElement(name = "HSI_ContractInventoryModificationRS")
public class HSIContractInventoryModificationRS
extends CoreResponse
implements Serializable
{

private final static long serialVersionUID = 20070801L;
@XmlElement(name = "Status", required = true)
protected YesNo status;

/**
 * Gets the value of the status property.
 * 
 * @return
 *     possible object is
 *     {@link YesNo }
 *     
 */
public YesNo getStatus() {
    return status;
}

/**
 * Sets the value of the status property.
 * 
 * @param value
 *     allowed object is
 *     {@link YesNo }
 *     
 */
public void setStatus(YesNo value) {
    this.status = value;
}

public boolean isSetStatus() {
    return (this.status!= null);
}

}  

If i unmarsh this
for XML 1

 HSIContractDetailModificationRQ hsi =  (HSIContractDetailModificationRQ) jaxbUnMarshallerForChannel.unmarshal(new StreamSource(new java.io.StringReader(xmlString)));

i get Exception :

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://axis.frontend.hsi.hotelbeds.com", local:"HSI_ContractDetailModificationRQ"). Expected elements are <{}HSI_ContractDetailModificationRQ>,<{}HSI_ContractDetailModificationRS>,<{}HSI_ContractInventoryModificationRQ>,<{}HSI_ContractInventoryModificationRS>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:238)  

If i unmarsh this
for XML 2

 HSIContractInventoryModificationRS  hsi = (HSIContractInventoryModificationRS)   jaxbContextForChannel.createUnmarshaller().unmarshal(new StreamSource(new java.io.StringReader(xmlInventoryRs)));

i get Exception :

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.hotelbeds.com/schemas/2005/06/messages", local:"HSI_ContractInventoryModificationRS"). Expected elements are <{}HSI_ContractDetailModificationRQ>,<{}HSI_ContractDetailModificationRS>,<{}HSI_ContractInventoryModificationRQ>,<{}HSI_ContractInventoryModificationRS>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:647)

I have seen in some site , it suggests to use package-info.java:

@XmlSchema(namespace = "http://axis.frontend.hsi.hotelbeds.com",    
elementFormDefault  =    
XmlNsForm.QUALIFIED)
  package com.cultagent4.hotelbeds.sa;
 import javax.xml.bind.annotation.*;    

I have used this , and xml 1 unmarshler works fine, but xml2 marshing fails .
Please help.

I am struggling with JAXB & XJC to build bindings to handle namespaces. I spent a lot of time on forums and googling. It does not works which makes me mad.

Atlast i have found a solution in a blog to ignore the namespace.
here is the solution link

Provided JAXB does not take into account namesapace of xml elements, it wrote the following code in order to ignore namespace while parsing.

JAXBContext jc = JAXBContext.newInstance("com.jmex.model.collada.collada_schema_1_4");
            Unmarshaller u = jc.createUnmarshaller();
            SAXParserFactory parserFactory;
            parserFactory = SAXParserFactory.newInstance();
            parserFactory.setNamespaceAware(false);
            XMLReader reader = parserFactory.newSAXParser().getXMLReader();
            Source er = new SAXSource(reader, new InputSource(sourceStream));
            HSIContractInventoryModificationRS hsi = (HSIContractInventoryModificationRS) u.unmarshal(er);

It works, JAXB unmarshalling is successful. (I hope this piece of code can help someone...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM