簡體   English   中英

如何解組不同XML中具有多個名稱空間的XML或忽略jaxb中的名稱空間

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

我有兩個XML和相應的Java文件(由trang.jar和xjc工具創建)

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>  

xjc創建的java文件:

@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>   

xjc創建的java文件:

@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);
}

}  

如果我解開這個
用於XML 1

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

我得到異常:

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)  

如果我解開這個
用於XML 2

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

我得到異常:

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)

我在某些站點上看到過,它建議使用package-info.java:

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

我使用了它,並且xml 1 unmarshler可以正常工作,但是xml2 marshing失敗。
請幫忙。

我正在努力與JAXB和XJC建立綁定來處理名稱空間。 我在論壇和谷歌搜索上花費了大量時間。 它不起作用,這使我生氣。

Atlast我在博客中找到了一種解決方案,可以忽略命名空間。
這是解決方案鏈接

提供的JAXB不考慮xml元素的名稱空間,它編寫了以下代碼,以便在解析時忽略名稱空間。

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);

它工作正常,JAXB解組成功。 (我希望這段代碼可以對某人有所幫助...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM