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>
阅读全文
0 0
- springmvc转换xml数据
- springmvc 数据转换
- SpringMVC数据转换
- springmvc之数据转换
- springmvc转换json数据
- SpringMVC -Controller返回xml数据
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换)
- java xml数据转换
- springMVC数据绑定、类型转换
- springmvc 数据转换与格式化
- SpringMVC ----数据进行类型转换
- SpringMVC 请求数据封装转换
- SpringMVC的数据转换、数据格式化
- SpringMVC信息转换--处理XML和JSON
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上)
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(下)
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(上)
- SpringMVC中使用@RequestBody,@ResponseBody注解实现Java对象和XML/JSON数据自动转换(下)
- 微信小程序 GMT+0800 (中国标准时间) WXSS 文件编译错误
- HTML语言制作表格
- 极大似然估计 EM算法
- Find the top5 similar issues in bugzilla system
- 以太币还能挖多久
- springmvc转换xml数据
- 【TensorFlow】激活函数(Activation Functions)原理解析(十二)
- Java正则匹配emoji表情
- cookie/session storage/local storage/地址栏传递
- weblogic 安装及相关的配置
- 电影出票系统感想
- 自定义 Laravel (monolog)日志位置
- php环境手动搭建
- JAVA 截取字符串的三种方法