我有两个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>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="MinimumStay" type="{}MinimumStayModRQ2" maxOccurs="unbounded"/>
* </sequence>
* </restriction>
* </complexContent>
* </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);
}
}
如果我没有骂这个 for 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)
如果我没有骂这个 for 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失败 请帮忙。
答案 0 :(得分:0)
我正在努力与JAXB&amp; XJC构建绑定以处理名称空间。我花了很多时间在论坛和谷歌搜索。它不起作用让我发疯。
Atlast我在博客中找到了一个忽略命名空间的解决方案 here is the solution link
如果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解组是成功的。 (我希望这段代码可以帮助某人......)