I have a requirement to merge two xmls generated from two different SOAP calls. Both the XMLs have different namespaces in them. I used wsimport tool to generate JAXB compliant classes frm two sepearte WSDLs. One of the classes in each generated package has a "Response" object which has an API to to retrieve the root node / element from the XML. My code works when I unmarshal each single xml for e.g. below code works, but when I combine it with another xml which has different namespace and different root node, it will not work for some reason. I tried to create a @XmlRootElement Class CdmData {...} which contains getters to populate the data from merged XML after unmarshalling, however, it does not work. Can anyone help what is wrong with me code here?
try {
JAXBContext jaxbContext = JAXBContext.newInstance(ContractRetrievalResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
contractAppData = ((JAXBElement<ContractRetrievalResponse>) jaxbUnmarshaller.unmarshal(new StreamSource(xmlFile1), ContractRetrievalResponse.class)).getValue();
} catch (JAXBException e) {
Logger.error(ContractAppCdmData.class, "Error! System cannot unmarshal an xml file.");
e.printStackTrace();
}
I have merged below xmls into one xml called LoanContractMerge.xml.... The issue is, I am not able to unmarshal the merged xml. I tried to use @XmlElmentRoot and @XmlElementWrapper as well, but they didn't seem to be working for my code. Below are the details:
**XML 1:**
<?xml version="1.0" encoding="UTF-8"?>
<ns3:SingleFamilyLoanFullViewResponse
xmlns:ns3="http://www.somewebsite.com/loanApp/ws/messages" xmlns:ns2="http://www.somewebsite.com/cdm">
<SingleFamilyLoanFullViews>
<SingleFamilyLoanFullView>
<SingleFamilyLoanSourcingData>
........
........
<ns2:Loan>
<ns2:LoanIdentifier>267731134</ns2:LoanIdentifier>
<ns2:LoanMaturityDate>2045-01-01</ns2:LoanMaturityDate>
<ns2:LoanPurposeType>Purchase</ns2:LoanPurposeType> <ns2:LoanScheduledFirstPaymentDate>2015-02-01</ns2:LoanScheduledFirstPaymentDate>
<ns2:MIDASLoanIdentifier>728254719</ns2:MIDASLoanIdentifier>
</ns2:Loan>
........
........
</SingleFamilyLoanSourcingData>
</SingleFamilyLoanFullView>
</SingleFamilyLoanFullViews>
</ns3:SingleFamilyLoanFullViewResponse>
****XML 2:****
<?xml version="1.0" encoding="UTF-8"?>
<p1:ContractRetrievalResponse xmlns:p="http://www.somelocation.com/cdm"
xmlns:p1="com/somelocation/contractapp/service">
<p1:LoanPurchaseContractStructure>
<p:ContractContainer>
........
........
<p:Contract>
<p:ContractDescription>p:ContractDescription</p:ContractDescription>
<p:ContractEffectiveDate>2001-12-31T12:00:00
</p:ContractEffectiveDate>
<p:ContractExpirationDate>2001-01-01</p:ContractExpirationDate>
<p:ContractIdentifier>41954028</p:ContractIdentifier>
<p:ContractInitiatingDepartmentIdentifier>p:ContractInitiatingDepartmentIdentifier
</p:Contract>
........
........
</p:ContractContainer>
</p1:LoanPurchaseContractStructure>
</p1:ContractRetrievalResponse>
***Java Class:***
@XmlRootElement(name = "cdmData")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CdmData", propOrder = {
"loanPurchaseContractStructure", "singleFamilyLoanFullViews",
"errors"
})
public class CdmData {
@XmlElement(name = "SingleFamilyLoanFullViews")
protected SingleFamilyLoanFullViews singleFamilyLoanFullViews;
@XmlElement(name = "Errors")
protected ErrorList errors;
@XmlElement(name = "LoanPurchaseContractStructure", nillable = true)
protected List<LoanPurchaseContractStructure> loanPurchaseContractStructure;
public SingleFamilyLoanFullViews getSingleFamilyLoanFullViews() {
return singleFamilyLoanFullViews;
}
public void setSingleFamilyLoanFullViews(SingleFamilyLoanFullViews value) {
this.singleFamilyLoanFullViews = value;
}
public List<LoanPurchaseContractStructure> getLoanPurchaseContractStructure() {
if (loanPurchaseContractStructure == null) {
loanPurchaseContractStructure = new ArrayList<LoanPurchaseContractStructure>();
}
return this.loanPurchaseContractStructure;
}
}
**Class with unmarshal method:**
public class CDMDataFactory {
........
........
String xmlFileName = "LoanContractMerge.xml";
FileReader xmlFileReader = null;
URL url = ClassLoader.getSystemResource(xmlFileName);
try {
xmlFileReader = new FileReader(url.getFile());
} catch (Exception ex) {
ex.printStackTrace();
} }
CdmData cdmData = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(CdmData.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
cdmData = ((JAXBElement<CdmData>) jaxbUnmarshaller
.unmarshal(new StreamSource(xmlFileReader),
CdmData.class)).getValue();
} catch (JAXBException e) {
Logger.error(CDMDataFactory.class,
"Error! System cannot unmarshal an xml file.");
e.printStackTrace();
}
.......
.......
} //CDMFactory.java class
I am not sure why I am not able to unmarshal the merged xml. When I use only one xml, it works as expected. Below are the xsd files.
**XSD for XML 1:**
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.somelocation.com/loanApp/ws/messages" targetNamespace="http://www.somelocation.com/loanApp/ws/messages" xmlns:cdm="http://www.somelocation.com/cdm" version="1.0">
<!-- <xs:include schemaLocation="LoanAPPRetrievalServiceBaseType.xs"/> -->
<xs:import namespace="http://www.somelocation.com/cdm" schemaLocation="SingleFamilyLoanAPPStructure.1.0.xsd"/>
<xs:element name="LoanAppSmartSearchRequest" type="LoanAppSmartSearchRequest" />
<xs:element name="SingleFamilyFullLoanViewRequest" type="SingleFamilyFullLoanViewRequest" />
<xs:element name="LoanAppSmartSearchResponse" type="LoanAppSmartSearchResponse" />
<xs:element name="SingleFamilyLoanFullViewResponse" type="SingleFamilyLoanFullViewResponse" />
<xs:element name="LoanServiceRuntimeFault" type="LoanServiceRuntimeFault" />
.................
.................
.................
<xs:complexType name="SingleFamilyLoanFullView">
<xs:sequence>
<xs:element name="SingleFamilyLoanSourcingData" type="cdm:SingleFamilyLoanContainer" minOccurs="0"/>
<xs:element name="SingleFamilyLoanServicingData" type="cdm:SingleFamilyLoanContainer" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="SingleFamilyLoanFullViews">
<xs:sequence>
<xs:element name="SingleFamilyLoanFullView" type="SingleFamilyLoanFullView" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="SingleFamilyLoanFullViewResponse">
<xs:sequence>
<xs:element name="SingleFamilyLoanFullViews" type="SingleFamilyLoanFullViews" minOccurs="0"/>
<xs:element name="Errors" type="ErrorList" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="LoanServiceRuntimeFault">
<xs:sequence>
<xs:element name="LoanServiceRuntimeFault" type="Fault_Type" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Fault_Type">
<xs:sequence>
<xs:element name="reason" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
.................
.................
</xs:schema>
**XSD for XML 2:**
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.somelocation.com/cdm" xmlns:cdm="http://www.somelocation.com/cdm" targetNamespace="http://www.somelocation.com/cdm" elementFormDefault="qualified" version="6.0">
<xs:include schemaLocation="cdm_ContractAPP.xsd"/>
<xs:include schemaLocation="security_content.structure_v1.xsd"/>
<xs:element name="LoanPurchaseContractStructure" type="LoanPurchaseContractStructure"/>
<xs:complexType name="LoanPurchaseContractStructure">
<xs:sequence>
<xs:element name="ContractContainer" type="ContractContainer" nillable="true" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ContractContainer">
<xs:sequence>
<xs:sequence minOccurs="1" maxOccurs="1" >
<xs:element name="Contract" type="Contract" nillable="true" minOccurs="1" maxOccurs="1" />
<xs:element name="ContractStatuses" type="ContractStatuses" nillable="true" minOccurs="1" maxOccurs="unbounded"/>
</xs:sequence>
<xs:element name="LongTermStandbyContract" type="LongTermStandbyContract" nillable="true" minOccurs="0"/>
<xs:element name="ContractLoans" type="ContractLoans" nillable="true" minOccurs="0"/>
<xs:element name="ContractAttributesContainers" type="ContractAttributesContainers" nillable="true" minOccurs="0" />
<xs:element name="LoanPurchaseContractContainer" type="LoanPurchaseContractContainer" nillable="true" minOccurs="0" />
<xs:element name="LoanServicingContractContainer" type="LoanServicingContractContainer" nillable="true" minOccurs="0" />
<!--<xs:any namespace="##any" processContents="skip" minOccurs="0" />-->
<xs:element name="SecurityContentContainer" type="SecurityContentContainer" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ContractAttributesContainers">
<xs:sequence>
<xs:element name="ContractAttributesContainer" type="ContractAttributesContainer" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ContractStatuses">
<xs:sequence>
<xs:element name="ContractStatus" type="ContractStatus" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ContractLoans">
<xs:sequence>
<xs:element name="Loan" type="Loan" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ContractAttributesContainer">
<xs:sequence>
<xs:element name="ContractSequenceAttributes" type="ContractSequenceAttributes" nillable="true" minOccurs="0"/>
<xs:element name="ContractAttribute" type="ContractAttribute" nillable="true" minOccurs="0"/>
<xs:element name="ServiceAttributeDefinition" type="ServiceAttributeDefinition" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ContractSequenceAttributes">
<xs:sequence>
<xs:element name="ContractAttributeSequenceCount" type="FMNumberInt" nillable="true" minOccurs="1"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="LoanPurchaseContractContainer">
<xs:sequence>
<xs:element name="LoanPurchaseContract" type="LoanPurchaseContract" nillable="true" minOccurs="0"/>
<xs:element name="LoanPurchaseContractPrice" type="LoanPurchaseContractPrice" nillable="true" minOccurs="0"/>
<xs:element name="SecuritySwapObligations" type="SecuritySwapObligations" nillable="true" minOccurs="0"/>
<xs:element name="LoanProductInstrument" type="LoanProductInstrument" nillable="true" minOccurs="0"/>
<xs:element name="LoanDeliveryObligation" type="LoanDeliveryObligation" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="SecuritySwapObligations">
<xs:sequence>
<xs:element name="SecuritySwapObligation" type="SecuritySwapObligation" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="LoanServicingContractContainer">
<xs:sequence>
<xs:element name="LoanServicingObligation" type="LoanServicingObligation" nillable="true" minOccurs="0"/>
<xs:element name="LoanServicingTerms" type="LoanServicingTerms" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>