已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年双鸭山市四方台区公务员人员招聘考试备考试题与答案详解
- 2026北京宣武医院神经外科招聘考试备考题库及答案解析
- 2026年合肥产投康养集团有限公司及子公司社会招聘12名(一线、专业技术岗位)笔试参考题库及答案解析
- 2026浙江省义乌市中心医院公开招聘48名编外工作人员(第二批)笔试模拟试题及答案解析
- 2026年安宁市林业和草原局招聘第二批编外聘用人员(防扑火大队队员)(22人)笔试备考题库及答案解析
- 2026集美区宁宝小学产假顶岗教师招聘1人考试备考题库及答案解析
- 2026年齐齐哈尔市富拉尔基区公务员人员招聘笔试参考题库与答案详解
- 2026年宜昌市西陵区公务员人员招聘笔试参考题库与答案详解
- 2026年北京市通州区公务员人员招聘笔试备考题库与答案详解
- 2027国家电网有限公司华东分部高校毕业生招聘(国调网调及北交提前批)考试模拟试题及答案解析
- 中医医院卒中中心脑出血救治流程
- 《股票价值评估用》课件
- 2027贵州航天电器秋季招聘284人考试备考题库及答案解析
- 地理信息数据分类分级工作指南试行全文
- 再生水回用处理工程设计
- 【新教材核心素养】教科版科学五年级上册3.3《用弹力驱动小车》教学课件
- 企业车间主任(班组长)岗位安全职责培训
- 小学五年级英语语法讲义
- 2026中铝物资面向中铝集团内部招聘9人备考题库及答案详解(全优)
- GB/T 21125-2026食用菌品种选育技术规范
- 基本公共卫生支出制度
评论
0/150
提交评论