有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javai是JAXB的新手。如何将两个具有不同名称空间的不同xml合并为一个具有根元素的xml并将其解组?

我需要合并由两个不同SOAP调用生成的两个XML。这两个XML中都有不同的名称空间。我使用wsimport工具从两个单独的wsdl生成与JAXB兼容的类。每个生成的包中的一个类都有一个“Response”对象,该对象有一个从XML中检索根节点/元素的API。当我为下面的代码解组每个xml时,我的代码是有效的,但当我将其与另一个具有不同名称空间和不同根节点的xml组合时,由于某种原因,它将不起作用。我试图创建一个@XmlRootElement类CdmData{…}它包含用于在解组后从合并的XML填充数据的getter,但是,它不起作用。 有人能帮我吗这里的代码出了什么问题

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

我已经将以下xml合并为一个名为LoanContractMerge的xml。xml。。。。问题是,我无法解组合并的xml。我试图使用@xmlementroot和@xmlementwrapper,但它们似乎对我的代码不起作用。详情如下:

**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) 个答案