JAXB示例

/**************1. 新建PO,对属性加入JAXB注释**********************/
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "学生")
public class Sutdent{
 @XmlElement(name="姓名")
 private String name;

 //TODO
 getter

 //TODO
 setter

}


/*******************************2. 将XML字符串转化成PO对象**************************/
XML字符串:
<?xml version="1.0" encoding="GB2312" ?>
<学生>
 <姓名>王小明</姓名>
</学生>

 

JAXBContext context = JAXBContext.newInstance(Sutdent.class)
Unmarshaller unmarshaller = context.createUnmarshaller();
Sutdent s = (Sutdent) unmarshaller.unmarshal(new StringReader(XML字符串));

你可能感兴趣的:(xml,JAXB)