开发者

Xml structure with XStream

开发者 https://www.devze.com 2023-01-18 22:44 出处:网络
I have class: public class EnglishWord implements Serializable, Comparable, Cloneable { static Logger logger = Logger.getLogger(EnglishWord.class);

I have class:


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;

.... }

I have Set words = new TreeSet(); And I use XStream for serialization:


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

After this I get structure of file like this:

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

Can I get something like this with XStream:


     <englishWord word="the" occurenceNumber="7480" rating="1" isVerb="true">
       <builtFrom class="tree-set" >
         <no-comparator/ >
         <stri开发者_StackOverflow中文版ng >The </string >
         <string >the </string >
       </builtFrom >
      </englishWord >

???


To convert them to attributes try the following:

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


Yes it is possible. Look at the Alias Tutorial and you want the Attribute Alias section.

0

精彩评论

暂无评论...
验证码 换一张
取 消