使用JAXBContext轻松实现Java和xml的互相转换方式
JAXBContext实现Java和xml的互相转换
类文件注解:@XmlRootElement不可缺少
@XmlRootElementpublic class Man {private String name; private int age; public Student() {super(); } public Student(String name, int age) {super(); this.name = name; this.age = age; }public String getName() {return name; }public void setName(String name) {this.name = name; }public int getAge() {return age; }public void setAge(int age) {this.age = age; }}
Java 转换 Xml:
@Testpublic void testJava2Xml(){try {JAXBContext jc = JAXBContext.newInstance(Man.class); Marshaller ms = jc.createMarshaller(); Man man = new Man("man",100); ms.marshal(man, System.out); } catch (JAXBException e) {e.printStackTrace(); }}
Xml 转换 Java:
@Testpublic void testXml2Java() throws JAXBException{String xml = "100 "; JAXBContext jc = JAXBContext.newInstance(Man.class); Unmarshaller unmar = jc.createUnmarshaller(); Man man = (Man) unmar.unmarshal(new StringReader(xml)); System.out.println(man.getName()); }man
JAXBContext 解析 xml 前提:
package com.sgcc.load.vo; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "gldxresult")@XmlAccessorType(XmlAccessType.FIELD)public class XmlAnalysis { // 泛化, 聚合 @XmlElement(name = "success") private String success; @XmlElement(name = "totalnum") private String totalnum; @XmlElement(name = "accounts")List accounts = new ArrayList<>(); public List getAccounts() {return accounts; } public void setAccounts(List accounts) {this.accounts = accounts; } public String getSuccess() {return success; } public void setSuccess(String success) {this.success = success; } public String getTotalnum() {return totalnum; } public void setTotalnum(String totalnum) {this.totalnum = totalnum; }}
package com.sgcc.load.vo; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; @SuppressWarnings("serial")@XmlAccessorType(XmlAccessType.FIELD)public class Accounts extends ArrayList{@XmlElement(name = "account")public List getAccount() {return this; }}
package com.sgcc.load.vo; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; @XmlAccessorType(XmlAccessType.FIELD)public class Account { @XmlElement(name = "seqid") private String seqid; @XmlElement(name = "dxid")private String dxid; @XmlElement(name = "compid")private String compid; @XmlElement(name = "rspcode")private String rspcode; @XmlElement(name = "code")private String code; @XmlElement(name = "name")private String name; public String getSeqid() {return seqid; } public void setSeqid(String seqid) {this.seqid = seqid; } public String getDxid() {return dxid; } public void setDxid(String dxid) {this.dxid = dxid; } public String getCompid() {return compid; } public void setCompid(String compid) {this.compid = compid; } public String getRspcode() {return rspcode; } public void setRspcode(String rspcode) {this.rspcode = rspcode; } public String getCode() {return code; } public void setCode(String code) {this.code = code; } public String getName() {return name; } public void setName(String name) {this.name = name; }}
测试:
package com.sgcc.load.test; import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import com.sgcc.load.vo.Accounts; import com.sgcc.load.vo.XmlAnalysis; import groovyjarjarcommonscli.ParseException; public class Test { private static XmlAnalysis transToVOs(String webserviceMsg) throws JAXBException, ParseException {final StringReader reader = new StringReader(webserviceMsg); JAXBContext ctx=JAXBContext.newInstance(new XmlAnalysis().getClass()); Unmarshaller um=ctx.createUnmarshaller(); XmlAnalysis ds=(XmlAnalysis)um.unmarshal(reader); return ds; } public static void main(String[] args) {String xmlMsg = " \n0 "; try{XmlAnalysis transToVOs = transToVOs(xmlMsg); for(int index = 0; index1 "+"hahaha 1136005118 0603 New0001 值:00 没有匹配到分类 "+"EHBSXPZYFDC000
哈哈哈哈哈哈哈哈哈
【使用JAXBContext轻松实现Java和xml的互相转换方式】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
推荐阅读
- 详解Spring|详解Spring Cache使用Redisson分布式锁解决缓存击穿问题
- PyTorch中的torch.nn模块使用详解
- python神经网络使用Keras进行模型的保存与读取
- python中|python中 @的含义以及基本使用方法
- 使用Hbuilder打包app
- 高级T-SQL进阶系列 上篇(使用 APPLY操作符)
- PyTorch二维张量使用图解
- RT-Thread|RT-Thread 应用篇 — 在STM32L051上使用 RT-Thread (五、无线温湿度传感器 之 完结篇)
- RT-Thread|RT-Thread 应用篇 — 在STM32L051上使用 RT-Thread (四、无线温湿度传感器 之 串口通讯)
- Android 选项菜单Option Menu 使用