2014-01-22 74 views
0

我使用XStream序列化我的實體,但我的實體有一個雙字段,我需要使用逗號作爲小數點標記序列化雙字段。使用XStream和雙逗號

在下面的代碼,拉斯特羅是雙:

XStream x = new XStream(); 
    x.autodetectAnnotations(true); 

    x.alias("Log", LogBook.class);      // 
    x.alias("LogBook", Log.class); 

    x.aliasField("IDLogbook" , LogBook.class, "idLogBook"); 
      x.aliasField("Lastro" , LogBook.class, "lastroKg"); 

File file = new File(CAMINHO_XML); 
    FileOutputStream save; 
    try { 
     save= new FileOutputStream(file); 
     save.write(x.toXML(l).getBytes()); 
     save.close(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
+0

您可以包括你需要什麼它看起來像一個例子嗎? – user314104

+0

2.2675

+0

我需要這個 2,2675

回答

-2

我解決了使用此代碼這個問題:

public static void serializeObject(Log l, String Pression,i nt ID) { 

    XStream x = new XStream(); 
    x.autodetectAnnotations(true); 

    x.alias("Log", LogBook.class);      // rename o root node or the class 
    x.alias("LogBook", Log.class); 

    x.aliasField("IDLogbook" , LogBook.class, "idLogBook"); // rename attribute class 
    x.aliasField("DateDive" , LogBook.class, "date"); 
    x.aliasField("Local" , LogBook.class, "local"); 
    x.aliasField("Contry" , LogBook.class, "Contry"); 
    x.aliasField("City" , LogBook.class, "city"); 
    x.aliasField("State" , LogBook.class, "State"); 
    x.aliasField("boddy" , LogBook.class, "boddy"); 
    x.aliasField("operator" , LogBook.class, "operator"); 
    x.aliasField("modality" , LogBook.class, "modality"); 
    x.aliasField("category" , LogBook.class, "category"); 
    x.aliasField("qualityDive" , LogBook.class, "qualityDive"); 
    x.aliasField("UsedTable" , LogBook.class, "UsedTable"); 
    x.aliasField("hourBegin" , LogBook.class, "hourBegin"); 

    x.aliasField("PEA" , LogBook.class, "PEA"); 
    x.aliasField("PPO2" , LogBook.class, "PPO2"); 

    x.aliasField("visibility" , LogBook.class, "visibility"); 
    x.aliasField("Manômetro" , LogBook.class, "manometro"); 
    x.aliasField("cilindermodel" , LogBook.class, "cilindermodel"); 
    if (Pressao.equals("Bar")){ 
     x.aliasField("PressionIN" , LogBook.class, "pressionInBar"); 
     x.aliasField("PressionOUT" , LogBook.class, "pressionOutBar"); 
    }else{ 
     x.aliasField("PressionIN" , LogBook.class, "pressionInPsi");  
     x.aliasField("PressionOUT" , LogBook.class, "pressionOutPsi"); 
    } 

    x.aliasField("Oxigen" , LogBook.class, "Oxigen"); 
    x.aliasField("Nitorgen" , LogBook.class, "Nitorgen"); 

    x.omitField(LogBook.class, "tipoPressionPSI"); // turn field hold 
    x.omitField(LogBook.class, "eanX"); 

    x.alias("Caverna", LogBookCaverna.class); 

    x.aliasField("IDLogbook" , LogBookCaverna.class, "idLogBook"); // renomeia o atributo da classe 
    x.aliasField("NomeCaverna" , LogBookCaverna.class, "nomeCaverna");  
    x.aliasField("ComoChegar" , LogBookCaverna.class, "comoChegar"); 
    x.aliasField("Profundidade" , LogBookCaverna.class, "profundidadeM"); 
    x.aliasField("Penetração" , LogBookCaverna.class, "penetracaoM"); 
    x.aliasField("RotaUsada" , LogBookCaverna.class, "rotaUsada"); 
    x.aliasField("EquipEspeologiaUsado" , LogBookCaverna.class, "equipUsado"); 
    x.aliasField("Obs" , LogBookCaverna.class, "obs"); 
    x.omitField(LogBookCaverna.class, "penetracaoMaxConhecidaM"); // omite o atributo da classe 
    x.omitField(LogBookCaverna.class, "penetracaoMaxConhecidaF"); 
    x.omitField(LogBookCaverna.class, "profundidadeMaxConhecidaM"); 
    x.omitField(LogBookCaverna.class, "profundidadeMaxConhecidaF");  
    x.omitField(LogBookCaverna.class, "idLogBookCaverna"); 
    x.omitField(LogBookCaverna.class, "LogBookCaverna"); 

    x.alias("Complement", LogBookComplement.class); 

    x.aliasField("Complemento" , LogBookComplemento.class, "complemento"); 
    x.aliasField("IDLogbook" , LogBookComplemento.class, "idLogBook"); // renomeia o atributo da classe 
    x.aliasField("Tempo" , LogBookComplemento.class, "tempo"); 
    x.aliasField("Entrada" , LogBookComplemento.class, "entrada"); 
    x.aliasField("TipoÁgua" , LogBookComplemento.class, "tipoAgua"); 
    x.aliasField("CondiçãoMar" , LogBookComplemento.class, "condicoesMar"); 
    x.aliasField("Acesso" , LogBookComplemento.class, "acessoLocal"); 
    x.aliasField("Facilidades" , LogBookComplemento.class, "facilidades"); 
    x.aliasField("Dificuldades" , LogBookComplemento.class, "dificuldades");   
    x.aliasField("Corrente" , LogBookComplemento.class, "correnteMaritima"); 
    x.aliasField("Obs" , LogBookComplemento.class, "obs"); 
    x.omitField(LogBookComplemento.class, "idLogBookComplemento"); 
    x.omitField(LogBookCaverna.class, "LogBookComplemento");  

    x.alias("Custos", LogBookCustos.class); 
    x.aliasField("IDLogbook" , LogBookCustos.class, "idLogBook"); // renomeia o atributo da classe 
    x.aliasField("CustoViagem" , LogBookCustos.class, "custoViagem"); 
    x.aliasField("CustoEmbarcação" , LogBookCustos.class, "custoEmbarcacao"); 
    x.aliasField("CustoEquipamentos" , LogBookCustos.class, "custoEquipamentos"); 
    x.aliasField("CustoDeslocamento" , LogBookCustos.class, "custoDeslocamento"); 
    x.aliasField("CustosDiversos" , LogBookCustos.class, "custoDiversos"); 
    x.aliasField("Obs" , LogBookCustos.class, "obs"); 
    x.omitField(LogBookCustos.class, "logBook"); 
    x.omitField(LogBookCustos.class, "custoFinal"); 

    x.alias("Embarcação", LogBookEmbarcacao.class); 
    x.aliasField("IDLogbook" , LogBookEmbarcacao.class, "IDLogbook"); 
    x.aliasField("NomeEmbarcação" , LogBookEmbarcacao.class, "nomeEmbarcacao"); 
    x.aliasField("Comprimento" , LogBookEmbarcacao.class, "comprimentoM"); 
    x.aliasField("CapitãoMestre" , LogBookEmbarcacao.class, "capitaoMestre"); 
    x.aliasField("NúmMergulhadores" , LogBookEmbarcacao.class, "numMergulhadores"); 
    x.aliasField("Obs" , LogBookEmbarcacao.class, "obs"); 
    x.omitField(LogBookEmbarcacao.class, "idLogBookEmbarcacao"); 
    x.omitField(LogBookEmbarcacao.class, "logBook"); 
    x.omitField(LogBookEmbarcacao.class, "equipSeguranca"); 
    x.omitField(LogBookEmbarcacao.class, "velocidade"); 
    x.omitField(LogBookEmbarcacao.class, "oxigenio"); 
    x.omitField(LogBookEmbarcacao.class, "camarotes"); 
    x.omitField(LogBookEmbarcacao.class, "banheiro"); 
    x.omitField(LogBookEmbarcacao.class, "salaTV"); 
    x.omitField(LogBookEmbarcacao.class, "detalhesEmbarque"); 
    x.omitField(LogBookEmbarcacao.class, "qualidadePlataforma"); 

    x.alias("Naufrágio", LogBookNaufragio.class); 
    x.aliasField("IDLogbook" , LogBookNaufragio.class, "IDLogbook"); 
    x.aliasField("NomeNaufrágio" , LogBookNaufragio.class, "nomeNaufragio"); 
    x.aliasField("VisibilidadeMédia" , LogBookNaufragio.class, "IDLogbook"); 
    x.aliasField("Penetração" , LogBookNaufragio.class, "IDLogbook"); 
    x.aliasField("Comprimento" , LogBookNaufragio.class, "comprimentoM"); 
    x.aliasField("ComoAfundou" , LogBookNaufragio.class, "IDLogbook"); 
    x.aliasField("TipoNaufrágio" , LogBookNaufragio.class, "tipoNaufragio"); 
    x.aliasField("TipoEmbarcação" , LogBookNaufragio.class, "tipoEmbarcacao"); 
    x.aliasField("ProfPopa" , LogBookNaufragio.class, "popaM"); 
    x.aliasField("ProfProa" , LogBookNaufragio.class, "proaM"); 
    x.aliasField("Obs" , LogBookNaufragio.class, "obs"); 
    x.omitField(LogBookNaufragio.class, "propulsao"); 
    x.omitField(LogBookNaufragio.class, "peso"); 
    x.omitField(LogBookNaufragio.class, "comprimentoF"); 
    x.omitField(LogBookNaufragio.class, "bocaM"); 
    x.omitField(LogBookNaufragio.class, "bocaF"); 
    x.omitField(LogBookNaufragio.class, "lancadoEm"); 
    x.omitField(LogBookNaufragio.class, "afundouEm"); 
    x.omitField(LogBookNaufragio.class, "popaF"); 
    x.omitField(LogBookNaufragio.class, "proaF"); 


    x.aliasField("IDLogbook" , LogBookFauna.class, "idLogBook"); 
    x.aliasField("Fauna" , LogBookFauna.class, "descricaoFauna"); 

    String CAMINHO_XML = Environment.getExternalStorageDirectory() + "/Dive/logbook_" + ID + ".xml"; 


    File arquivo = new File(CAMINHO_XML); 

    FileOutputStream gravar; 
    try { 
     gravar = new FileOutputStream(arquivo); 
     //gravar.write(x.toXML(l).getBytes()); 
     gravar.write(x.toXML(l).toString().replace(".", ",").getBytes()); 
     gravar.close(); 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 

} 
+0

我投這個答案了,因爲解決這個問題正是基於這一行:'gravar.write(x.toXML(L)的ToString()代替(」 。「,」,「)。getBytes());'。這個解決方案非常脆弱:'l'中的每個字段都被序列化爲一個包含「。」的字符串。現在將所有的「。」替換爲「,」。例如,如果'l'包含一個字段'private String comment;',並且'comment'填充了''Example text,end。「',那麼這個答案中的序列化代碼會將它變成」Example text,end, 「無法恢復。 – user314104

+0

我使用替換,因爲我需要在其他程序中導入此xml,而此其他程序使用「,」就像十進制值。 –

+0

看看我上面提出的答案;它向/從Double值提供轉換,以小數分隔符(基於德語區域設置)發出「,」。目前的代碼只是格式化'Double',但是這個方法也可以擴展到'Float'類型。請注意,我的示例代碼不執行「。」的全面轉換。到「,」,所以它沒有表現出這裏討論的不足。 – user314104

2

什麼你要找的是爲Double類的XStream,對XStream的實例註冊自定義AbstractSingleValueConverter

import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.text.NumberFormat; 
import java.text.ParseException; 
import java.util.Locale; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class App { 
    // Placeholder class 
    public static class LogBook { 

    } 
    // Placeholder class 
    public static class Log { 

    } 

    public static final String CAMINHO_XML = "cahminho.xml"; 

    public static void serialize(LogBook l) { 
     XStream x = new XStream(); 

     LocaleAwareDoubleConverter c = new LocaleAwareDoubleConverter(); 

     // Set your desired locale here; German happens to use the desired format 
     c.setLocale(Locale.GERMAN); 
     x.registerConverter(c); 

     x.autodetectAnnotations(true); 

     x.alias("Log", LogBook.class); 
     x.alias("LogBook", Log.class); 

     x.aliasField("IDLogbook", LogBook.class, "idLogBook"); 
     x.aliasField("Lastro", LogBook.class, "lastroKg"); 

     File file = new File(CAMINHO_XML); 
     FileOutputStream save; 
     try { 
      save = new FileOutputStream(file); 
      save.write(x.toXML(l).getBytes()); 
      save.close(); 
     } catch (IOException ex) { 
      Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    // Remove the static qualifier to put this class into a separate file 
    public static class LocaleAwareDoubleConverter extends AbstractSingleValueConverter { 

     private NumberFormat format = NumberFormat.getInstance(); 

     public void setLocale(Locale l) { 
      format = NumberFormat.getInstance(l); 
     } 

     public boolean canConvert(Class clazz) { 
      return clazz.equals(Double.class); 
     } 

     // This is where the magic happens... 
     public String toString(Object o) { 
      return format.format((Double) o); 
     } 

     // And to turn the string back into a Double, we do this 
     public Object fromString(String str) { 
      try { 
       return format.parse(str).doubleValue(); 
      } catch (ParseException ex) { 
       Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); 
       throw new IllegalArgumentException(str, ex); 
      } 
     } 
    } 
} 

選擇合適的NumberFormat根據你爲了執行正確的格式轉換的願望Locale。示例中的LocaleAwareDoubleConverter類使用它來轉換爲正確的格式。