我有一个方法,在该方法中我将XmlObject作为参数接收,现在我想将其转换为与Java相对应的java对象,我必须将其传递给其他Web服务。

我尝试了所有可能的方法,但无法获得。

代码:

public static boolean updateAddress_V2(XmlObject xmlObject) throws XmlException{
         AlarsDataService dataService=new AlarsDataService();
         CustomerV2 customerV2=CustomerV2.Factory.parse(xmlObject.toString());
         com.alars.osb.java.customer.CustomerV2.Customer customerXML=customerV2.getCustomer();
}


但是当我检查customerXML即将为空时。

这是XMLObject字符串值:

 <Customer_V2 xmlns="http://www.alars.com/osb/java/Customer">
  <Customer>
    <customerId>4</customerId>
    <driverLicense>XBRT245</driverLicense>
    <firstName>ALEX</firstName>
    <lastName>CINTRA</lastName>
    <dob>21-11-1986</dob>
    <addressLine1>10 Florence St</addressLine1>
    <city>BOSTON</city>
    <zipCode>02148</zipCode>
  </Customer>
</Customer_V2>


客户XSD:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.alars.com/osb/java/Customer"
    xmlns:tns="http://www.alars.com/osb/java/Citation" elementFormDefault="qualified">
    <complexType name="Customer">
        <sequence>
            <element name="customerId" type="long"></element>
            <element name="driverLicense" type="string"></element>
            <element name="firstName" type="string"></element>
            <element name="lastName" type="string"></element>
            <element name="dob" type="date"></element>
            <element name="addressLine1" type="string"></element>
            <element name="city" type="string"></element>
            <element name="zipCode" type="string"></element>
        </sequence>
    </complexType>

    <complexType name="Customer_V2">
        <sequence>
            <element name="Customer">
                <complexType>
                    <sequence>
                        <element name="customerId" type="long"></element>
                        <element name="driverLicense" type="string"></element>
                        <element name="firstName" type="string"></element>
                        <element name="lastName" type="string"></element>
                        <element name="dob" type="date"></element>
                        <element name="addressLine1" type="string"></element>
                        <element name="city" type="string"></element>
                        <element name="zipCode" type="string"></element>
                    </sequence>
                </complexType>
            </element>
        </sequence>
    </complexType>
</schema>


任何劝告乡亲..如何实现这一目标?

最佳答案

您将XmlObject的架构类型更改为必需的CustomerV2,如下所示,因为我们不知道前面的类型。

CustomerV2 customerV2 = (CustomerV2)
xmlObject.changeType(CustomerV2.type);


要根据所需的CustomerV2架构类型检查架构类型,可以执行以下操作。

xmlObject.schemaType().isAssignableFrom(CustomerV2 .type);

10-06 12:55