我是JAXB的新手。如何将具有不同命名空间的两个不同的xmls合并到一个带有根元素的xml中并解组它?

时间:2015-06-10 00:41:32

标签: java jaxb unmarshalling

我需要合并两个不同SOAP调用生成的两个xmls。这两个XML都有不同的命名空间。我使用wsimport工具生成两个sepearte WSDL的JAXB兼容类。每个生成的包中的一个类具有“Response”对象,该对象具有用于从XML检索根节点/元素的API。当我解组每个单独的xml时,我的代码可以工作。下面的代码工作,但当我将它与另一个具有不同命名空间和不同根节点的xml结合使用时,由于某种原因它将无法工作。我试图创建一个@XmlRootElement类CdmData {...},其中包含getters以在解组后填充合并XML中的数据,但是,它不起作用。 任何人都可以帮助我解决我的代码错误吗?

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

我已将xmls合并到一个名为LoanContractMerge.xml的xml中....问题是,我无法解组合并的xml。我也尝试使用@XmlElmentRoot和@XmlElementWrapper,但它们似乎不适用于我的代码。以下是详细信息:

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

0 个答案:

没有答案