2011-11-04 91 views
7

我有一個定義爲xsd:double的元素。如果我嘗試將一個值放入元素285,然後我編組它,我會得到一個285.0的輸出......這很好。但是,如果我在285292746中輸入一個值,那麼當輸出爲2.85292746E8時我是元帥。 我喜歡一些東西,這樣雙重輸出不會被轉換成帶十進制的科學記數法?基本上我想285292746或2852292746.0000000Jaxb:全局綁定使用XMLAdapter進行雙重類型轉換

java.lang.Double.toString()使用「計算機科學記數法」爲某些值生成isml for xml。

我知道給定值的雙重表示法很好。但事實上它的價值是指數格式,我正在處理的系統是接受我的XML,但不知道如何處理指數值並導致我的程序無法正常工作。在WSDL或服務器中更改xsd:double類型對我來說是不可行的。我在客戶端工作。

我遇到了Jaxb:binding for xsd:double type。我仍然很難解決這個問題,以非價格格式發送價值雙重價值。

package com.logger.client 
import javax.xml.bind.annotation.adapters.XmlAdapter; 
import javax.xml.bind.DatatypeConverter; 

public class JaxbDoubleSerializer extends XmlAdapter<String, Double> 
{ 


    public Double unmarshal(String value) { 
     return ((double)javax.xml.bind.DatatypeConverter.parseDouble(value)); 
    } 

    public String marshal(Double value) { 
     if (value == null) { 
      return null; 
     } 
     return (javax.xml.bind.DatatypeConverter.printDouble((double)(double)value)); 
    } 

} 

我需要幫助如何使用DoubleSerializer,以便我可以傳遞沒有指數的值。我嘗試在我的MyLogClient.java類中使用xmlAdapter Annotation。我需要知道我能如何解決這種情況。

package com.logger.client 
import javax.xml.ws.BindingProvider; 
import javax.xml.bind.JAXBElement;import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.XmlValue; 


public class MyLogClient 
{ 
    //Private member fields 

    /** Object factory used to create user-and-role specific objects. */ 
    private static final ObjectFactory _of = new ObjectFactory(); 

    @XmlJavaTypeAdapter(JaxbDoubleSerializer.class) 
    public JAXBElement<Double> msgFileId; 

     @XmlJavaTypeAdapter(JaxbDoubleSerializer.class) 
    public Double dNumber; 


public final void createEntry; 
    (
     final String userName, 
     final String time, 
     final String logMsgStringId, 
     final Params logMsgParamsVal, 
     final Integer logMessageFieldID 
    ) 

    throws JAXBException 
    { 

     JAXBElement<String> username = _of.createParamsParam(userName); 
     JAXBElement<String> strTime  = _of.createLogRequestTime(time); 


     // Build the LogRequest request. 
     final LogRequest _LogRequest = _of.createLogRequest(); 
     _LogRequest.setUserName(userName); 
     _LogRequest.setTime(strTime); 

     //Following is the main problem 
     int nMsgArgs = 285292746; 
     dNumber = Double.parseDouble(Integer.toString(nMsgArgs)); 
     //After parsing double Value I get dNumber is now 2.85292746E8 

     //MsgFile Id is of Type JAXBElement<Double> 
     //CreateLogMessageIdentifier takes Double param 
     //So the main problem is here..the value of double field in soap request 
     //is being sent in exponential format. I need to send as I explained above 
     //285292746. 

     msgFileId = _of.createLogMessageIdentifier(dNumber); 

     JAXBElement<String> strIdVal = _of.createLogMessageFileId(logMsgStringId); 

     final LogMessage logMessage = _of.createLogMessage(); 

    JAXBElement<Params> _logMsgParams =_of.createLogMessageParams(logMsgParamsVal); 

    //Following is where I am trying to use marshall for double type. 

    JAXBContext context = JAXBContext.newInstance("package com.logger.client "); 
    context.createMarshaller().marshal(msgFileId, System.out); 

     logMessage.setIdentifier(msgFileId); //Method takes JAXBElement<Double> 

     logMessage.setFileId(strIdVal); 
     logMessage.setParams(_logMsgParams); 

     JAXBElement<LogMessage> logMsgValue = _of.createLogRequestLogMessage(logMessage); 


     _LogRequest.setLogMessage(logMsgValue); 

     // Set the log entry 
     port.log(_LogRequest); //Request is sent to server. 

}

WSDL的xsd:類型聲明低於: -

<xsd:complexType name="LogMessage"> 
     <xsd:sequence> 
     <xsd:element name="fileId" type="xsd:string" minOccurs="0" nillable="true" /> 
     <xsd:element name="identifier" type="xsd:double" minOccurs="0" nillable="true" /> 
     <xsd:element name="params" type="tns:Params" minOccurs="0" nillable="true" /> 
     </xsd:sequence> 
      </xsd:complexType> 

輸出,用於識別符字段是: -

 <identifier> 2.85292746E8</indentifier> 

Whereas I want to send as. Because system does accept/recognize following types. 

<identifier> 285292746</indentifier> 
or 
<identifier> 285292746.00000000</indentifier> 

回答

1

的最佳方式可能是使用一個使用JAXB編譯器自定義綁定(javaType子句)。文檔可以在here找到。取決於你是否需要編組和解組雙打,解決方案可能很簡單,就像在全球範圍內將映射覆蓋到諸如BigDecimal之類的東西一樣簡單(雖然雙精度和小數是完全不同的東西,但你似乎想要一個十進制表示),或使用數據類型轉換器(上面的鏈接爲您提供了所有這些選項的示例)。

+0

你會舉一些具體的例子嗎?你會認爲,XML適配器選項是好的。我試圖使用上面在我的例子JAxDouble Serialzer中所示,但不知道我錯過了什麼。 – user1029083

7

你可以使用一個外部綁定文件類似如下:

binding.xml

<jxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
    version="2.1"> 

    <jxb:globalBindings> 
      <jxb:javaType name="java.lang.Double" 
       xmlType="xs:double" 
       parseMethod="javax.xml.bind.DatatypeConverter.parseDouble" 
       printMethod="javax.xml.bind.DatatypeConverter.printDouble" /> 
    </jxb:globalBindings> 

</jxb:bindings> 

根。XSD

<?xml version="1.0"?> 
<xs:schema 
    elementFormDefault="qualified" 
    targetNamespace="http://www.example.com" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:complexType name="root"> 
     <xs:sequence> 
      <xs:element name="foo" type="xs:double" /> 
      <xs:element name="bar" type="xs:double" /> 
     </xs:sequence> 
    </xs:complexType> 

</xs:schema> 

XJC呼叫

xjc -d out -b binding.xml root.xsd 

根類型生成將對雙重屬性上註冊XmlAdapter類的類:

package com.example; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlSchemaType; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 
import org.w3._2001.xmlschema.Adapter1; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "root", propOrder = { 
    "foo", 
    "bar" 
}) 
public class Root { 

    @XmlElement(required = true, type = String.class) 
    @XmlJavaTypeAdapter(Adapter1 .class) 
    @XmlSchemaType(name = "double") 
    protected Double foo; 
    @XmlElement(required = true, type = String.class) 
    @XmlJavaTypeAdapter(Adapter1 .class) 
    @XmlSchemaType(name = "double") 
    protected Double bar; 

    public Double getFoo() { 
     return foo; 
    } 

    public void setFoo(Double value) { 
     this.foo = value; 
    } 

    public Double getBar() { 
     return bar; 
    } 

    public void setBar(Double value) { 
     this.bar = value; 
    } 

} 

適配器1

XmlAdapter使用您在binding.xml文件中配置的方法。

package org.w3._2001.xmlschema; 

import javax.xml.bind.annotation.adapters.XmlAdapter; 

public class Adapter1 extends XmlAdapter<String, Double> { 

    public Double unmarshal(String value) { 
     return (javax.xml.bind.DatatypeConverter.parseDouble(value)); 
    } 

    public String marshal(Double value) { 
     if (value == null) { 
      return null; 
     } 
     return (javax.xml.bind.DatatypeConverter.printDouble(value)); 
    } 

} 
+0

非常感謝。我會嘗試這種方式。我沒有xsd文件,但源代碼是從wsdl文件生成的,該文件通過URL發佈。我假設我將不得不使用wsimport或其他方法來綁定wsdl。 xjc -d out -b binding.xml(http://localhost/MyClient.wsdl)或wsimport -d out -b binding.xml location.wsdl。 – user1029083