springmvc转换xml数据

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;

@Controller
public 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;

@Controller
public 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>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值