简体   繁体   English

如何解组不同XML中具有多个名称空间的XML或忽略jaxb中的名称空间

[英]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和相应的Java文件(由trang.jar和xjc工具创建)

XML 1 : 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 : 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 : 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 : 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);
}

}  

If i unmarsh this 如果我解开这个
for XML 1 用于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 用于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: 我在某些站点上看到过,它建议使用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 . 我使用了它,并且xml 1 unmarshler可以正常工作,但是xml2 marshing失败。
Please help. 请帮忙。

I am struggling with JAXB & XJC to build bindings to handle namespaces. 我正在努力与JAXB和XJC建立绑定来处理名称空间。 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. Atlast我在博客中找到了一种解决方案,可以忽略命名空间。
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. 提供的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);

It works, JAXB unmarshalling is successful. 它工作正常,JAXB解组成功。 (I hope this piece of code can help someone...) (我希望这段代码可以对某人有所帮助...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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