已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- CCAA - 2016年12月环境管理体系基础答案及解析 - 详解版(100题)
- CCAA - 2013服务标准化与服务认证(机构)答案及解析 - 详解版(29题)
- 养老院紧急情况处理制度
- 企业员工培训与发展制度
- 浙江省事业单位考试职业能力倾向测验(医疗卫生类E类)应考要点详解
- 我国上市公司治理结构、信息不对称与自愿性信息披露的联动效应及优化路径研究
- 重金属回转窑焙烧工操作规范考核试卷含答案
- 插秧机操作工安全宣教模拟考核试卷含答案
- 遗体火化师安全强化测试考核试卷含答案
- 乙炔发生工安全实操水平考核试卷含答案
- 福建省宁德市2025-2026学年高三上学期期末考试语文试题(含答案)
- 建筑施工行业2026年春节节前全员安全教育培训
- 食品生产余料管理制度
- 2026年浦发银行社会招聘备考题库必考题
- 2026届高考语文复习:小说人物形象复习
- 脱碳塔CO2脱气塔设计计算
- 产品报价单货物报价表(通用版)
- 疱疹性咽峡炎临床路径
- 中学保安工作管理制度
- 内蒙古品味自然农牧业公司VI设计理念
- 上腔静脉综合征的护理
评论
0/150
提交评论