package main;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.json.XML;
import com.alibaba.fastjson.JSONObject;
import com.sun.xml.internal.bind.marshaller.CharacterEscapeHandler;
public class XmlUtil {
public static void main(String[] args) throws Exception {
Property property = new Property(9999, 9999, 9999);
List<Equip> equip = new ArrayList<Equip>();
equip.add(new Equip(9999,"青龙偃月刀"));
equip.add(new Equip(9999,"黄金铠甲"));
User user = new User("关羽", "男", 23, "关云长", property, equip);
String xml = javaBeanToXml(user);
System.out.println(xml);
User xmlToJavaBean = xmlToJavaBean(xml);
System.out.println(xmlToJavaBean);
}
/**
*
* @Title: javaBeanToXml
* @Description: javaBean转xml
* @param @param user
* @param @return
* @param @throws Exception
* @return String
* @throws
*/
public static String javaBeanToXml(User user) throws Exception {
StringWriter sw = new StringWriter();
// 利用jdk中自带的转换类实现
JAXBContext context = JAXBContext.newInstance(user.getClass());
Marshaller marshaller = context.createMarshaller();
// 格式化xml输出的格式
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// 去掉生成xml的默认报文头
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
//转义特殊符号
marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
public void escape(char[] ch, int start,int length, boolean isAttVal, Writer writer) throws IOException {
writer.write(ch, start, length);
}
});
// 将对象转换成输出流形式的xml
marshaller.marshal(user, sw);
sw.close();
return sw.toString();
}
/**
*
* @Title: xmlToJavaBean
* @Description: xml转javaBean
* @param @param xml
* @param @return
* @param @throws Exception
* @return User
* @throws
*/
public static User xmlToJavaBean(String xml) throws Exception {
org.json.JSONObject jsonObject = XML.toJSONObject(xml).getJSONObject("xml");
User user = JSONObject.parseObject(jsonObject.toString(), User.class);
return user;
}
}
/**
*
* @author mrzhang
* CDATA 指的是不由 XML 解析器进行解析的文本数据.
* XML 元素中,"<" 和 "&" 是非法的。
*/
class CDataAdapter extends XmlAdapter<String, String> {
//从javabean到xml的适配方法
@Override
public String marshal(String str) throws Exception {
return "<![CDATA[" + str+ "]]>";
}
//从xml到javabean的适配方法
@Override
public String unmarshal(String str) throws Exception {
return str;
}
}
@XmlRootElement( name = "xml")
class User {
private String name;
private String gender;
private Integer age;
private String nickname;
private Property property;
private List<Equip> equips;
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String name, String gender, Integer age, String nickname,
Property property, List<Equip> equips) {
super();
this.name = name;
this.gender = gender;
this.age = age;
this.nickname = nickname;
this.property = property;
this.equips = equips;
}
@XmlElement(name = "Name")
@XmlJavaTypeAdapter(CDataAdapter.class)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "Gender")
@XmlJavaTypeAdapter(CDataAdapter.class)
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@XmlElement(name = "Age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@XmlElement(name = "Nickname")
@XmlJavaTypeAdapter(CDataAdapter.class)
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@XmlElement(name = "Property")
public Property getProperty() {
return property;
}
public void setProperty(Property property) {
this.property = property;
}
// @XmlElementWrapper(name = "Equips")
@XmlElement( name = "Equips")
public List<Equip> getEquips() {
return equips;
}
public void setEquips(List<Equip> equips) {
this.equips = equips;
}
@Override
public String toString() {
return "User [name=" + name + ", gender=" + gender + ", age=" + age
+ ", nickname=" + nickname + ", property=" + property
+ ", equips=" + equips + "]";
}
}
class Property{
private Integer hp;
private Integer attack;
private Integer defense;
public Property() {
super();
// TODO Auto-generated constructor stub
}
public Property(Integer hp, Integer attack, Integer defense) {
super();
this.hp = hp;
this.attack = attack;
this.defense = defense;
}
@XmlElement(name = "HP")
public Integer getHp() {
return hp;
}
public void setHp(Integer hp) {
this.hp = hp;
}
@XmlElement(name = "Attack")
public Integer getAttack() {
return attack;
}
public void setAttack(Integer attack) {
this.attack = attack;
}
@XmlElement(name = "Defense")
public Integer getDefense() {
return defense;
}
public void setDefense(Integer defense) {
this.defense = defense;
}
@Override
public String toString() {
return "Property [hp=" + hp + ", attack=" + attack + ", defense="
+ defense + "]";
}
}
class Equip{
private Integer attack;
private String name;
public Equip() {
super();
// TODO Auto-generated constructor stub
}
public Equip(Integer attack, String name) {
super();
this.attack = attack;
this.name = name;
}
@XmlElement(name = "Attack")
public Integer getAttack() {
return attack;
}
public void setAttack(Integer attack) {
this.attack = attack;
}
@XmlElement(name = "Name")
@XmlJavaTypeAdapter(CDataAdapter.class)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Equip [attack=" + attack + ", name=" + name + "]";
}
}