java - 如何将 org.apache.xmlbeans.XmlObject 转换为我的 customJavaObject

标签 java xml apache xsd xmlbeans

我有一个方法,我在该方法中接收 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);

关于java - 如何将 org.apache.xmlbeans.XmlObject 转换为我的 customJavaObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20554639/

相关文章:

java - Android从XML获取图像url到ImageView

c# - 如何使用带有 WebMatrix 的 C#.net 网页访问单个 XML 元素的值?

xml - Grails 错误 : invalid XML character (Unicode: 0x5c)

apache - 在 Ubuntu 16.04.7 上将 Apache 2.4.18 升级到 2.4.26 或更高版本

apache - 您可以相对于 conf 文件设置 Apache 的 DocumentRoot 吗?

java - 服务层中的Spring @Validated

java - 如何在 main 中的所有声明对象中仅选择该数组中的随机元素一次?

java - 在运行时更改标题的颜色

Java编译器树api : NPE

apache - Seafile 安装 - {"detail":"Authentication credentials were not provided."}