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