how to Convert org.apache.xmlbeans.XmlObject to my customJavaObject

4.9k views Asked by At

I am having a method where I am receiving XmlObject as an argument in the method and now I want to convert it into the java corresponding java object , that I have to pass in other webservice.

I have tried all the possible ways but not able to get it.

Code :

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

but when I am checking customerXML is coming as null.

Here is the XMLObject string value :

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

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

Any advise folks.. how to achieve this ??

2

There are 2 answers

0
joethep On

You change the schema type of XmlObject to required CustomerV2 as follows since we do not know the type ahead.

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

To check the schema type against the required CustomerV2 schema type, you can do the following.

xmlObject.schemaType().isAssignableFrom(CustomerV2 .type);
1
Peter Keller On

The XML does not match to the XSD. Especially, the XSD is missing the top element entries. Use following Schema instead and re-generate your XMLBeans classes:

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

Parse your XML as follows:

final CustomerParentDocument customerParentV1 = CustomerParentDocument.Factory.parse(file);