2013-12-12 19 views
0

我有一種方法,我在方法中接受XmlObject作爲參數,現在我想將它轉換爲java對應的java對象,我必須通過其他網絡服務。如何將org.apache.xmlbeans.XmlObject轉換爲我的customJavaObject

我嘗試了所有可能的方式,但無法得到它。

代碼:

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> 

任何建議人..如何實現這一目標?

+0

我的猜測是,要麼你的分析器壞了,或者你的XML的語法被打破。這個解析器有多靈活?我們可以看到它嗎?從我們所知道的,這段代碼完美地工作,你在撒謊(讀:沒有語法錯誤,假設你的類存在)。 – Izmaki

+0

更新了問題以包含我的customer.xsd。 – saurav

+0

我沒有看到任何改變:)(我們去了) – Izmaki

回答

1

您將XmlObject的模式類型更改爲所需的CustomerV2,因爲我們不知道前面的類型。

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

要根據所需的CustomerV2架構類型檢查架構類型,可以執行以下操作。

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

XML與XSD不匹配。特別是,XSD缺少頂部element條目。使用下面的架構,而不是和重新生成您的XMLBeans類:

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

分析XML如下:

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

@ tech-idiot我更新了我的答案。也許你可能會看看它。 –

相關問題