JAXB解析xml
2015-07-06 17:39
375 查看
废话不多说,直接上代码
核心类:
成员类:
成员类2:
xml文件:
核心类:
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> T readString(Class<T> 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> T readConfig(Class<T> 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> T readConfigFromStream(Class<T> 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<TestOrg> { // 泛化, 聚合 @XmlAttribute(name = "size") private int size; @XmlAttribute(name = "batch_number") private Long batchNumber; @XmlAttribute(name = "errmsg") private String errmsg; @XmlElement(name = "org") public List<TestOrg> 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; /** * <pre> * 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. * </pre> * * @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) { this.orgId = orgId; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { this.parentId = parentId; } public String getOrgName() { return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } public String getOrgCode() { return orgCode; } public void setOrgCode(String orgCode) { this.orgCode = orgCode; } public String getOrgType() { return orgType; } public void setOrgType(String orgType) { this.orgType = 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文件:
<?xml version="1.0" encoding="UTF-8"?> <orgs size="7095" batch_number="20130704110039" errmsg=""> <org> <org_id>16817</org_id> <parent_id>9233</parent_id> <org_name>512AAS.苏州沧浪区</org_name> <org_code>512AAS</org_code> <org_type>门店部门</org_type> <start_d>2011-12-28</start_d> <end_d></end_d> <attribute1></attribute1> <insert_t>2013-7-4 10:32:09</insert_t> </org> <org> <org_id>16817</org_id> <parent_id>9233</parent_id> <org_name>512AAS.苏州沧浪区</org_name> <org_code>512AAS</org_code> <org_type>门店部门</org_type> <start_d>2011-12-28</start_d> <end_d></end_d> <attribute1></attribute1> <insert_t>2013-7-4 10:32:09</insert_t> </org> <org> <org_id>16817</org_id> <parent_id>9233</parent_id> <org_name>512AAS.苏州沧浪区</org_name> <org_code>512AAS</org_code> <org_type>门店部门</org_type> <start_d>2011-12-28</start_d> <end_d></end_d> <attribute1></attribute1> <insert_t>2013-7-4 10:32:09</insert_t> </org> <org> <org_id>16817</org_id> <parent_id>9233</parent_id> <org_name>512AAS.苏州沧浪区</org_name> <org_code>512AAS</org_code> <org_type>门店部门</org_type> <start_d>2011-12-28</start_d> <end_d></end_d> <attribute1></attribute1> <insert_t>2013-7-4 10:32:09</insert_t> </org> <org> <org_id>16817</org_id> <parent_id>9233</parent_id> <org_name>512AAS.苏州沧浪区</org_name> <org_code>512AAS</org_code> <org_type>门店部门</org_type> <start_d>2011-12-28</start_d> <end_d></end_d> <attribute1></attribute1> <insert_t>2013-7-4 10:32:09</insert_t> </org> </orgs>
相关文章推荐
- apache转发客户端端口到tomcat的配置 以及 apache日志格式的配置
- (a+b)/2的位运算
- 例解 autoconf 和 automake 生成 Makefile 文件
- webclient下载文件 带进度条
- C++ 虚函数表解析
- Extjs读取本地下拉选框数据源,分为text和value,显示text,传值value
- mysql的union操作
- mac 删除文件夹里所有的.svn文件
- 代码隐藏模型
- frostwire设置libtorrent实现顺序下载边下边播
- git 使用一条龙
- mac下在eclipse中如何清除/切换svn
- Android轮询机制
- msdasql.dll was unable to register itself in the system registry.
- Qt实现应用程序单实例运行--LocalServer方式
- 集群的分类
- Linux中link,unlink,close,fclose详解
- Scala学习第一篇
- (剑指Offer)面试题6:重建二叉树
- vim