JAXB解析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>