XStream的Xml结构

时间:2010-10-05 08:14:39

标签: java xml xstream

我上课了:


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;

.... }

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

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;

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

  

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());
}

我可以使用XStream获得这样的东西:

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

???

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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