已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
核心类:package com.jaxb;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.text.MessageFormat;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;public class JaxbReadXml SuppressWarnings(unchecked) public static T readString(Class clazz, String context) throws JAXBException try JAXBContext jc = JAXBContext.newInstance(clazz); Unmarshaller u = jc.createUnmarshaller(); return (T) u.unmarshal(new File(context); catch (JAXBException e) / logger.trace(e); throw e; SuppressWarnings(unchecked) public static T readConfig(Class clazz, String config, Object. arguments) throws IOException, JAXBException InputStream is = null; try if (arguments.length 0) config = MessageFormat.format(config, arguments); / logger.trace(read configFileName= + config); JAXBContext jc = JAXBContext.newInstance(clazz); Unmarshaller u = jc.createUnmarshaller(); is = new FileInputStream(config); return (T) u.unmarshal(is); catch (IOException e) / logger.trace(config, e); throw e; catch (JAXBException e) / logger.trace(config, e); throw e; finally if (is != null) is.close(); SuppressWarnings(unchecked) public static T readConfigFromStream(Class clazz, InputStream dataStream) throws JAXBException try JAXBContext jc = JAXBContext.newInstance(clazz); Unmarshaller u = jc.createUnmarshaller(); return (T) u.unmarshal(dataStream); catch (JAXBException e) / logger.trace(e); throw e; public static void main(String args) throws JAXBException TestOrgs testOrgs = JaxbReadXml.readString(TestOrgs.class, test/test.xml); System.out.println(testOrgs.getSize(); System.out.println(testOrgs.getBatchNumber(); System.out.println(testOrgs.getErrmsg(); for (TestOrg o : testOrgs) System.out.println(o.getOrgName(); 成员类:package com.jaxb;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.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;SuppressWarnings(serial)XmlRootElement(name = orgs)XmlAccessorType(XmlAccessType.FIELD)public class TestOrgs extends ArrayList / 泛化, 聚合 XmlAttribute(name = size) private int size; XmlAttribute(name = batch_number) private Long batchNumber; XmlAttribute(name = errmsg) private String errmsg; XmlElement(name = org) public List getOrgs() return this; public int getSize() return size; public void setSize(int size) this.size = size; public Long getBatchNumber() return batchNumber; public void setBatchNumber(Long batchNumber) this.batchNumber = batchNumber; public String getErrmsg() return errmsg; public void setErrmsg(String errmsg) this.errmsg = errmsg; 成员类2:package com.jaxb;import java.util.Date;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;/* * * If you annotate your Artifact class with the annotation: * XmlAccessorType(XmlAccessType.FIELD) * then you do not need to annotate the fields with XmlElement and the * setter/getter methods will be ignored. * * * author User */XmlAccessorType(XmlAccessType.FIELD)/ 用了这个之后就会自动忽略setter/getter方法。不用这个就需要去掉注解,需要保证属性名和xml里的表签名一致public class TestOrg XmlElement(name = org_id) private Long orgId; XmlElement(name = parent_id) private Long parentId; XmlElement(name = org_name) private String orgName; XmlElement(name = org_code) private String orgCode; XmlElement(name = org_type) private String orgType; XmlElement(name = start_d) private Date startDate; XmlElement(name = end_d) private Date endDate; XmlElement(name = attribute1) private String attribute; XmlElement(name = insert_t) private Date insertTime; public Long getOrgId() return orgId; public void setOrgId(Long orgId) Id = orgId; public Long getParentId() return parentId; public void setParentId(Long parentId) this.parentId = parentId; public String getOrgName() return orgName; public void setOrgName(String orgName) Name = orgName; public String getOrgCode() return orgCode; public void setOrgCode(String orgCode) Code = orgCode; public String getOrgType() return orgType; public void setOrgType(String orgType) Type = orgType; public Date getStartDate() return startDate; public void setStartDate(Date startDate) this.startDate = startDate; public Date getEndDate() return endDate; public void setEndDate(Date endDate) this.endDate = endDate; public String getAttribute() return attribute; public void setAttribute(String attribute) this.attribute = attribute; public Date getInsertTime() return insertTime; public void setInsertTime(Date insertTime) this.insertTime = insertTime; xml文件: 16817 9233 512AAS.苏州沧浪区 512AAS 门店部门 2011-12-28 2013-7-4 10:32:09 16817 92
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年司法考试《法律基础知识与法律逻辑》备考题库及答案解析
- 16.2《六国论》课件统编版高一语文必修下册
- 2025-2030中国男士职业套装行业发展分析及投资价值预测研究报告
- 移动应用UI设计制作规范
- 2025年厨余垃圾处理主题公园设备选型
- 2025年世界知识产权日知识产权考试试题库及答案指导
- 2025年事业单位考试专业知识试题(附答案)
- 2026年人工智能模型训练协议
- 2026农产品出口行业市场供需分析及投资发展评估规划分析报告
- 2026农业科技企业成长路径分析及投融资策略研究
- 2026年北京市西城区初三一模英语试卷(含答案)
- 2026年38期入团考试题及答案
- 会当凌绝顶一览众山小 论文三篇(800字)
- GB/T 9163-2001关节轴承向心关节轴承
- GB/T 26163.1-2010信息与文献文件管理过程文件元数据第1部分:原则
- GA 270-2009警用服饰帽徽
- 习作:《我学会了-》课件
- 新概念英语 青少版入A U1-U9测试
- 烹饪工艺学原理课件
- 公司各部门工作流程图(通用)
- 骨质疏松量表
评论
0/150
提交评论