1. 先贴一下我们要实现的xml格式:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CONDITIONS>
<CONTITION exp="12" no="1" sid="12">
<bdmbkbh>2</bdmbkbh>
<fhjgjls>2</fhjgjls>
<rxxsd operation="!=">80</rxxsd>
<sjgliat>
<operation>=</operation>
<value>30</value>
</sjgliat>
<sjgliat>
<operation>1=</operation>
<value>20</value>
</sjgliat>
</CONTITION>
</CONDITIONS>
2. xml转对象,和对象转xml方法:
/**
* @param xmlStr 字符串
* @param c 对象Class类型
* @return 对象实例
*/
@SuppressWarnings("unchecked")
public static <T> T xml2Object(String xmlStr, Class<T> c) {
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
T t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
return t;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
/**
* @param object 对象
* @return 返回xmlStr
*/
public static String object2Xml(Object object) {
//这个地方传的是一个对象
try {
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化输出
marshal.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式,默认为utf-8
marshal.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xml头信息
marshal.setProperty("jaxb.encoding", "utf-8");
marshal.marshal(object, writer);
return new String(writer.getBuffer());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
3. 常用的注解:
@XmlAttribute(name = " ")
这个是 < 后面的值 ,例如:
<CONTITION exp="1*2" no="1" result="" sid="0012">
这个 exp no result 都是在实体类的get方法上 加上了这个注解 , name 后面是随意起的,不用和实体类的属性对应
@XmlValue
这个是用于这种情况<RXXSD operation="=">30</RXXSD>
我们把 RXSSD 当成一个对象,里面有两个属性值 operation 和 value,
在value的get方法上加上@XmlValue 注解,就不会显示 value=30 而是只显示 30
@XmlElement(name = "CONTITION")
这个注解用不用都一样,因为默认的会将对象名做为一个节点例如
<user>
</user>
但是也可以加上这个注解,这样可以将这个对象起任意的节点名字。
@XmlRootElement( name = "CONDITIONS" )
这个意思是标记这个实体类在最外层根节点
@XmlElements({ @XmlElement(name = "CSRQ", type = CSRQ.class) })
这个一般用不着,当出问题了,报找不到实体类路径的问题时候,就这样写
给对象赋值,然后用上面的方法:
object2Xml(Object)
详细代码如下:
实体类:
package demon;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mabin6 on 2018/8/31.
*/
@XmlRootElement( name = "CONDITIONS" )
public class conditions {
List<condition> conlist = new ArrayList<>();
@XmlElement(name = "CONTITION")
public List<condition> getConlist() {
return conlist;
}
public void setConlist(List<condition> conlist) {
this.conlist = conlist;
}
}
package demon;
import javax.xml.bind.annotation.XmlAttribute;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mabin6 on 2018/8/31.
*/
public class condition {
private String exp;
private String no;
private String sid;
private String bdmbkbh;
private String fhjgjls;
private rxxsd rxxsd;
private List<sjgsdw> sjgliat = new ArrayList<>();
@XmlAttribute(name = "exp")
public String getExp() {
return exp;
}
public void setExp(String exp) {
this.exp = exp;
}
@XmlAttribute(name = "no")
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
@XmlAttribute(name = "sid")
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getBdmbkbh() {
return bdmbkbh;
}
public void setBdmbkbh(String bdmbkbh) {
this.bdmbkbh = bdmbkbh;
}
public String getFhjgjls() {
return fhjgjls;
}
public void setFhjgjls(String fhjgjls) {
this.fhjgjls = fhjgjls;
}
public List<sjgsdw> getSjgliat() {
return sjgliat;
}
public void setSjgliat(List<sjgsdw> sjgliat) {
this.sjgliat = sjgliat;
}
public demon.rxxsd getRxxsd() {
return rxxsd;
}
public void setRxxsd(demon.rxxsd rxxsd) {
this.rxxsd = rxxsd;
}
}
package demon;
/**
* Created by mabin6 on 2018/8/31.
*/
public class sjgsdw {
private String operation;
private String value;
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package demon;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
/**
* Created by mabin6 on 2018/8/31.
*/
public class rxxsd {
private String operation;
private String value;
@XmlAttribute(name = "operation")
public String getOperation() {
return operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
@XmlValue
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
主函数:
package demon;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
/**
* Created by mabin6 on 2018/8/31.
*/
public class testMain {
public static void main(String[] args){
conditions conds = new conditions();
condition cdon = new condition();
List<condition> conlist = new ArrayList<>();
cdon.setBdmbkbh("2");
cdon.setExp("12");
cdon.setFhjgjls("2");
cdon.setNo("1");
cdon.setSid("12");
List<sjgsdw> sjgliat = new ArrayList<>();
sjgsdw sjdw1 = new sjgsdw();
sjdw1.setOperation("=");
sjdw1.setValue("30");
sjgliat.add(sjdw1);
sjgsdw sjdw2 = new sjgsdw();
sjdw2.setOperation("1=");
sjdw2.setValue("20");
sjgliat.add(sjdw2);
cdon.setSjgliat(sjgliat);
rxxsd rxsd = new rxxsd();
rxsd.setValue("80");
rxsd.setOperation(">");
cdon.setSjgliat(sjgliat);
cdon.setRxxsd(rxsd);
conlist.add(cdon);
conds.setConlist(conlist);
System.out.println(object2Xml(conds));
}
/**
* @param xmlStr 字符串
* @param c 对象Class类型
* @return 对象实例
*/
@SuppressWarnings("unchecked")
public static <T> T xml2Object(String xmlStr, Class<T> c) {
try {
JAXBContext context = JAXBContext.newInstance(c);
Unmarshaller unmarshaller = context.createUnmarshaller();
T t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
return t;
} catch (JAXBException e) {
e.printStackTrace();
return null;
}
}
/**
* @param object 对象
* @return 返回xmlStr
*/
public static String object2Xml(Object object) {
//这个地方传的是一个对象
try {
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshal = context.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // 格式化输出
marshal.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式,默认为utf-8
marshal.setProperty(Marshaller.JAXB_FRAGMENT, false);// 是否省略xml头信息
marshal.setProperty("jaxb.encoding", "utf-8");
marshal.marshal(object, writer);
return new String(writer.getBuffer());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
最后运行结果:
?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CONDITIONS>
<CONTITION exp="12" no="1" sid="12">
<bdmbkbh>2</bdmbkbh>
<fhjgjls>2</fhjgjls>
<rxxsd operation=">">80</rxxsd>
<sjgliat>
<operation>=</operation>
<value>30</value>
</sjgliat>
<sjgliat>
<operation>1=</operation>
<value>20</value>
</sjgliat>
</CONTITION>
</CONDITIONS>