使用的javax这个jar下的类,jaxb(java architecture for xml binding) 这个java和xml的互转标准如果感兴趣可以自行百度这里就不介绍概念,直接上代码。
首先是工具类,这里比较简单,就没做生成实体文件,就是两个,一个对象转xml,一个xml转对像。
import javax.xml.bind.jaxbcontext;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.hashmap;import java.util.list;import java.util.map;/** * @desc: xml 工具类 * @author: wan兀值表gzhh * @date: 2019-06-13 */public class xmluti藏族舞蹈教案ls { /** * description bean对像转换为xml字符串, * 对像属性如果不设置值,xml字符串没有节点,如果需要空节点,可以设置为空字符串 * @param obj 需要转换的对像,转换的对像需要做对应的注解 * @return java.lang.string * @author wangzhh * @date 2019-06-13 **/ public static string converttoxml(object obj) { // 输出流 stringwriter writer = new stringwriter(); try { jaxbcontext context = jaxbcontext.newinstance(obj.getclass()); marshaller marshaller = context.createmarshaller(); // 格式化xml输出的格式 marshaller.tproperty(marshaller.jaxb_formatted_output, boolean.true); //marshaller.tproperty(marshaller.jaxb_formatted_output, boolean.fal); // 设置编码集 marshaller.tproperty(marshaller.jaxb_encoding, "utf-8"); // 将对象转换成输出流形式的xml marshaller.m纳兰容若经典诗词arshal(obj, writer); } catch (exception e) { e.printstacktrace(); } return writer.tostring(); } /** * description xml字符串转换为bean对像 * @author wangzhh * @date 2019-06-13 * @param clazz * @param xmlstr * @return java.lang.object **/ public static object convertxmlstrtoobject(class clazz, string xmlstr) { object xmlobject = null; try { jaxbcontext context = jaxbcontext.newinstance(clazz); // 进行将xml转成对象 unmarshaller unmarshaller = context.createunmarshaller();打造什么样的团队 stringreader reader = new stringreader(xmlstr); xmlobject = unmarshaller.unmarshal(reader); } catch (exception e) { e.printstacktrace(); } return xmlobject; } public static void main(string[] args) { testxmlbean t = new testxmlbean(); t.tname("张三"); t.tage(""); map<string,object> map = new hashmap<string, object>(); map.put("map","mapvalue"); t.tmsgmap(map); list<testlistbean> listbean = new arraylist<testlistbean>(); testlistbean t1 = new testlistbean(); t1.taddress("一号地址"); te非洲巨蛙stlistbean t2 = new testlistbean(); t2.taddress("二号地址"); listbean.add(t1); listbean.add(t2); t.tlistbean(listbean); system.out.println(xmlutils.converttoxml(t)); string xmlstr = xmlutils.converttoxml(t); xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><test><name>张三</name><age></age><msgmap><entry><key>map</key><value>mapvalue</value></entry></msgmap><listbean><address>一号地址</address></listbean><listbean><address>二号地址</address></listbean></test>\n"; testxmlbean bean = (testxmlbean) xmlutils.convertxmlstrtoobject(testxmlbean.class,xmlstr); system.out.println(bean.tostring()); }}
测试用的类,饱含集合
import javax.xml.bind.annotation.xmlaccesstype;import javax.xml.bind.annotation.xmlaccessortype;import javax.xml.bind.annotation.xmlrootelement;import javax.xml.bind.annotation.xmltype;import java.io.rializable;import java.util.list;import java.util.map;/** * @desc: 工具类测试对像 * @author: wangzhh * @date: 2019-06-13 */@xmlaccessortype(xmlaccesstype.field)// 根节点@xmlrootelement(name = "test")// 需要转换的属性,顺序@xmltype(proporder = {"name","age","msgmap","listbean"})public class testxmlbean implements rializable { private static final long rialversionuid = -6854694430588003755l; private string name; private string age; private map<string,object> msgmap; private list<testlistbean> listbean; public string getname() { return name; } public void tname(string name) { this.name = name; } public string getage() { return age; } public void tage(string age) { this.age = age; } public map<string, object> getmsgmap() { return msgmap; } public void tmsgmap(map<string, object> msgmap) { this.msgmap = msgmap; } public list<testlistbean> getlistbean() { return listbean; } public void tlistbean(list<testlistbean> listbean) { this.listbean = listbean; } @override public string tostring() { return "testxmlbean{" + "name='" + name + '\'' + ", age='" + age + '\'' + ", msgmap=" + msgmap + ", listbean=" + listbean + '}'; }}
import javax.xml.bind.annotation.xmlaccesstype;import javax.xml.bind.annotation.xmlaccessortype;import javax.xml.bind.annotation.xmlrootelement;import javax.xml.bind.annotation.xmltype;/** * @desc: * @author: wangzhh * @date: 2019-06-13 */@xmlaccessortype(xmlaccesstype.field)// 根节点@xmlrootelement(name = "listbean")// 需要转换的属性@xmltype(proporder = {"address"})public class testlistbean { private string address; public string getaddress() { return address; } public void taddress(string address) { this.address = address; } @override public string tostring() { return "testlistbean{" + "address='" + address + '\'' + '}'; }}
测试结果,控制台成功输出
connected to the target vm, address: '127.0.0.1:49443', transport: 'socket'<?xml version="1.0" encoding="utf-8" standalone="yes"?><test> <name>张三</name> <age></age> <msgmap> <entry> <key>map</key> <value xsi:type="xs:string" xmlns:xs="/d/file/titlepic/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">mapvalue</value> </entry> </msgmap> <listbean> <address>一号地址</address> </listbean> <listbean> <address>二号地址</address> </listbean></test>testxmlbean{name='张三', age='', msgmap={map=[value: null]}, listbean=[testlistbe
本文发布于:2023-04-05 06:33:02,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/64b0308bf9f34e984dca04d1a1603b5f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:xml格式化工具有哪些(ue格式化xml快捷键).doc
本文 PDF 下载地址:xml格式化工具有哪些(ue格式化xml快捷键).pdf
留言与评论(共有 0 条评论) |