2016-04-20 78 views

回答

0

有一個名爲XStream的庫,專門用於此目的(XML到java或反之亦然)。

備用Soluton - JAXB。使用XStream的

示例程序 -

import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.annotations.XStreamAlias; 

public class ConverterExample3 { 
    static String xml1 = "<Music><type>rock</type></Music>"; 

    public static void main(String[] args) { 
     XStream xStream = new XStream(); 
     xStream.processAnnotations(Music.class); 
     Music musicObject = (Music) xStream.fromXML(xml1); 
     System.out.println(musicObject); 
     // prints Music [type=ROCK] 
    } 

} 

@XStreamAlias("Music") 
class Music { 
    public enum genre { 
     ROCK, JAZZ 
    }; 

    genre type; 

    @Override 
    public String toString() { 
     return "Music [type=" + type + "]"; 
    } 

} 
+0

好的,謝謝我試試 –