今天心血来潮,想实现xml到JavaBean的实现,baidu了下,发现了可以使用这个[url=http://code.google.com/p/xmappr/]xmappr[/url]来实现xml->javabean的转换,在此记录下。
book.xml
book.xml
<root a="2.2">
some text
<node>123</node>
</root>
import org.xmappr.Attribute;
import org.xmappr.Element;
import org.xmappr.RootElement;
import org.xmappr.Text;
@RootElement
public class Root {
@Attribute
public float a;
@Element
public Integer node;
@Text
public String text;
}
//读取当前src路径下的book.xml文件
InputStream in = new FileInputStream(new File(System.getProperty("user.dir")+"\\src\\book.xml"));
Reader reader = new BufferedReader(new InputStreamReader(in));;
Xmappr xm = new Xmappr(Root.class);
Root root = (Root) xm.fromXML(reader);
System.out.println(root.text);