把Object对象转换成XML格式的数据

1.在model包里创建StrObject.xsd文件:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn"
        elementFormDefault="qualified">
        <xs:element name="StrObject">
                <xs:complexType id="property">
                        <xs:sequence>
                                <xs:element name="returncode" type="xs:string" />
                                <xs:element name="queryresult" type="xs:string" />
                                <xs:element name="userid" type="xs:string" />
                                <xs:element name="userlevel" type="xs:string" />
                                <xs:element name="reason" type="xs:string" />
                                <xs:element name="type" type="xs:string" />
                                <xs:element name="format" type="xs:string" />
                        </xs:sequence>
                </xs:complexType>
        </xs:element>
</xs:schema>
2.在model包里创建StrObject.java类:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "returncode",
    "queryresult",
    "userid",
    "userlevel",
    "reason",
    "type",
    "format",
})
@XmlRootElement(name = "property")
public class DomainStatusCheckReturn {
         @XmlElement(required = true)
        private String returncode;
         @XmlElement(required = true)
        private String queryresult;
         @XmlElement(required = true)
        private String userid;
         @XmlElement(required = true)
        private String userlevel;
         @XmlElement(required = true)
        private String reason;
         @XmlElement(required = true)
        private String type;
         @XmlElement(required = true)
        private String format;
        public String getReturncode() {
                return returncode;
        }
        public void setReturncode(String returncode) {
                this.returncode = returncode;
        }
        public String getQueryresult() {
                return queryresult;
        }
        public void setQueryresult(String queryresult) {
                this.queryresult = queryresult;
        }
        public String getUserid() {
                return userid;
        }
        public void setUserid(String userid) {
                this.userid = userid;
        }
        
        public String getUserlevel() {
                return userlevel;
        }
        public void setUserlevel(String userlevel) {
                this.userlevel = userlevel;
        }
        public String getReason() {
                return reason;
        }
        public void setReason(String reason) {
                this.reason = reason;
        }
        public String getType() {
                return type;
        }
        public void setType(String type) {
                this.type = type;
        }
        public String getFormat() {
                return format;
        }
        public void setFormat(String format) {
                this.format = format;
        }
}
3.创建getObjectToXml方法(将对象转换成XML格式的文件)
public static <T> String getObjectToXml(T object) throws IOException
        {
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                try
                {
                        JAXBContext context = JAXBContext.newInstance(object.getClass());
                        // 将对象转变为xml Object------XML
                        // 指定对应的xml文件
                        Marshaller marshaller = context.createMarshaller();
                        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);//是否格式化生成的xml串
                marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);//是否省略xml头信息
                        
                        // 将对象转换为对应的XML文件
                        marshaller.marshal(object, byteArrayOutputStream);
                }
                catch (JAXBException e)
                {
                        
                        e.printStackTrace();
                }
                //转化为字符串返回
                String xmlContent = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
                return xmlContent;
        }

4.创建getXmlToObject方法(将XML类型的数据转换成对象)
public static <T> T getXmlToObject(String xmlContent,Class clazz)
        {
                try
                {
                        JAXBContext context = JAXBContext.newInstance(clazz);
                        // xml转换为对象 XML------Object
                        InputStream inputStream;
                        try
                        {
                                inputStream = new ByteArrayInputStream(xmlContent.getBytes("UTF-8"));
                                Unmarshaller um = context.createUnmarshaller();
                                
                                return (T) um.unmarshal(inputStream);
                        }
                        catch (UnsupportedEncodingException e)
                        {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                catch (JAXBException e)
                {
                        
                        e.printStackTrace();
                }
                return null;
        }

5.将获取的数据赋值给StrObject对象
StrObject strReturn=new StrObject();
                strReturn.setReturncode(list.get(0).get属性);
                strReturn.setQueryresult(list.get(0).get属性);
                strReturn.setUserid(list.get(0).get属性);
                strReturn.setUserlevel(list.get(0).get属性);
                strReturn.setReason(list.get(0).get属性);
                strReturn.setType(list.get(0).get属性);
                strReturn.setFormat(list.get(0).get属性);
5. 把XML数据显示在页面
public void ShowXml(){
String xmlDataString = getObjectToXml(strReturn);
                if(StringUtils.isNotBlank(xmlDataString))
                {
                        HttpServletResponse response = ServletActionContext.getResponse();
                        // 设置编码
                        response.setCharacterEncoding("UTF-8");
                        response.setContentType("text/plain;charset=utf-8");
                        response.setHeader("Cache-Control", "no-cache");
                        PrintWriter out = null;
                        try {
                                out = response.getWriter();
                                out.write(xmlDataString);
                                out.flush();
                        } catch (Exception e) {
                                e.printStackTrace();
                        }finally{
                                if(out != null)
                                        out.close();
                        }
                }
  }

原文地址:https://www.cnblogs.com/malcolm/p/2702987.html