【问题标题】:Xml structure with XStream带有 XStream 的 XML 结构
【发布时间】:2010-10-05 08:14:39
【问题描述】:

我有课:


public class EnglishWord implements Serializable, Comparable,
        Cloneable {
    static Logger logger = Logger.getLogger(EnglishWord.class);

private static final long serialVersionUID = -5832989302176618764L;
private String word;// in lowercase if not personal name
private int occurenceNumber = 1;// 0,1,2,3,
private int rating;// 1-first, 2 - second...
private Set<String> builtFrom;
private String translation;
private boolean isVerb;
private boolean isNoun;
private boolean isIrregular;

.... }

我有 Set words = new TreeSet(); 我使用 XStream 进行序列化:


XStream xs = new XStream();
xs.alias("englishWord", EnglishWord.class);
FileOutputStream fs = null;
try {
  fs = new FileOutputStream(fileName);
} catch (FileNotFoundException e) {
  logger.error(e.getMessage());
}
xs.toXML(words, fs);
try {
  fs.flush();
  fs.close();
} catch (IOException e) {
  logger.error(e.getMessage());
}

在此之后,我得到这样的文件结构:

   
     <englishWord>
       <word >the </word >
       <occurenceNumber >7480 </occurenceNumber >
       <rating >1 </rating >
       <builtFrom class="tree-set" >
         <no-comparator/ >
         <string >The </string >
         <string >the </string >
       </builtFrom >
       <isVerb >false </isVerb >
       <isNoun >false </isNoun >
       <isIrregular >false </isIrregular >
     </englishWord>

我可以用 XStream 得到这样的东西吗:


     <englishWord word="the" occurenceNumber="7480" rating="1" isVerb="true">
       <builtFrom class="tree-set" >
         <no-comparator/ >
         <string >The </string >
         <string >the </string >
       </builtFrom >
      </englishWord >

???

【问题讨论】:

    标签: java xml xstream


    【解决方案1】:

    要将它们转换为属性,请尝试以下操作:

    xs.useAttributeFor(EnglishWord.class, "word");
    xs.useAttributeFor(EnglishWord.class, "occurenceNumber");
    xs.useAttributeFor(EnglishWord.class, "rating");
    xs.useAttributeFor(EnglishWord.class, "isVerb");
    

    【讨论】:

      【解决方案2】:

      是的,这是可能的。查看Alias Tutorial,您需要“属性别名”部分。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 2019-05-01
        • 2017-07-20
        • 2011-11-21
        • 1970-01-01
        相关资源
        最近更新 更多