解决方法
有非常好的框架
JAXB为这也有
Simple
但是我已经使用了这个XStream
Person joe = new Person("Joe","Walnes"); joe.setPhone(new PhoneNumber(123,"1234-456")); joe.setFax(new PhoneNumber(123,"9999-999"));
现在,要将其转换为XML,您只需简单地调用XStream即可:
String xml = xstream.toXML(joe);
生成的XML如下所示:
<person> <firstname>Joe</firstname> <lastname>Walnes</lastname> <phone> <code>123</code> <number>1234-456</number> </phone> <fax> <code>123</code> <number>9999-999</number> </fax> </person>
另见
> JAXB
> where-i-can-find-a-detailed-comparison-of-java-xml-frameworks