springmvc转换xml数据

来源:互联网 发布:webpack压缩单个js文件 编辑:程序博客网 时间:2024/06/08 13:24

package model;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import java.io.Serializable;@XmlRootElement //表示xml文档的根元素public class Computer implements Serializable{    private String name ;    private String type ;    public Computer(){}    public Computer(String name ,String type){        super();        this.name = name;        this.type = type;    }    @XmlElement //该属性作为xml的element    public String getName(){        return name;    }    public void setName(String name){        this.name = name;    }    public String getType(){        return type;    }    @XmlElement //该属性作为xml的element    public void setType(String type){        this.type = type;    }    public String toString(){        return "Computer[name=]"+name+","+"[type=]"+type+"]";    }}

1接受xml格式的数据

<%@ page language="java" contentType="text/html; charset=UTF-8"         pageEncoding="UTF-8"%><!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>测试接收XML格式的数据</title>    <script type="text/javascript" src="../js/json2.js"></script>    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>    <script type="text/javascript">        $(document).ready(function(){            sendxml();        });        function sendxml(){            var xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><computer><name>惠普</name><type>12</type></computer>";            $.ajax("${pageContext.request.contextPath}/sendxml",// 发送请求的URL字符串。                {                    type : "POST", //  请求方式 POST或GET                    contentType:"application/xml", //  发送信息至服务器时的内容编码类型                    // 发送到服务器的数据。                    data: xmlData,                    async:  true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求                });        }    </script></head><body></body></html>
package controller;import model.Computer;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.xml.bind.JAXBContext;import javax.xml.bind.Unmarshaller;import java.io.InputStream;@Controllerpublic class ComputerController {    @RequestMapping(value="/sendxml" , method = RequestMethod.POST)    public void sendxml(@RequestBody Computer computer){        String str = new String();        System.out.println(computer);        System.out.println("接受到xml数据");    }    @RequestMapping(value="/readxml",method = RequestMethod.POST)    public @ResponseBody Computer readXml() throws Exception{        JAXBContext context = JAXBContext.newInstance(Computer.class);        Unmarshaller unmarshaller = context.createUnmarshaller();        InputStream is =this.getClass().getResourceAsStream("/computer.xml");        //进行xml到Java之间对象的转换        Computer computer = (Computer) unmarshaller.unmarshal(is);        return computer;    }}

2返回xml格式的数据


<%@ page language="java" contentType="text/html; charset=UTF-8"         pageEncoding="UTF-8"%><!DOCTYPE html><html><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>测试返回XML格式的数据</title>    <script type="text/javascript" src="../js/json2.js"></script>    <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>    <script type="text/javascript">        $(document).ready(function(){            readxml();        });        function readxml(){            $.ajax("${pageContext.request.contextPath}/readxml",// 发送请求的URL字符串。                {                    dataType : "text", // 预期服务器返回的数据类型。                    type : "POST", //  请求方式 POST或GET                    async:  true , // 默认设置下,所有请求均为异步请求。如果设置为false,则发送同步请求                    // 请求成功后的回调函数。                    success :function(xml){                        // 获得xml数据的id,name,author                        var name = $("name", xml).text();                        var type = $("type", xml).text();                        var tr  = $("<tr align='center'/>");                        $("<td/>").html(name).appendTo(tr);                        $("<td/>").html(type).appendTo(tr);                        $("#booktable").append(tr);                    },                    // 请求出错时调用的函数                    error:function(){                        alert("数据接收失败");                    }                });        }    </script></head><body><table id="booktable" border="1"  style="border-collapse: collapse;">    <tr align="center">        <th>名称</th>        <th>类型</th>    </tr></table></body></html>

package controller;import model.Computer;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.xml.bind.JAXBContext;import javax.xml.bind.Unmarshaller;import java.io.InputStream;@Controllerpublic class ComputerController {       @RequestMapping(value="/readxml",method = RequestMethod.POST)    public @ResponseBody Computer readXml() throws Exception{        JAXBContext context = JAXBContext.newInstance(Computer.class);        Unmarshaller unmarshaller = context.createUnmarshaller();        InputStream is =this.getClass().getResourceAsStream("/computer.xml");        //进行xml到Java之间对象的转换        Computer computer = (Computer) unmarshaller.unmarshal(is);        return computer;    }}

computer.xml:

<?xml version="1.0" encoding="UTF-8"?><computer><name>apple</name><type>macbookpro</type></computer>




原创粉丝点击