廢話(huà)不多說(shuō),直接上代碼
核心類(lèi):
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()); } }}
成員類(lèi):
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; }}
成員類(lèi)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)// 用了這個(gè)之后就會(huì )自動(dòng)忽略setter/getter方法。不用這個(gè)就需要去掉注解,需要保證屬性名和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>門(mén)店部門(mén)</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>門(mén)店部門(mén)</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>門(mén)店部門(mén)</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>門(mén)店部門(mén)</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>門(mén)店部門(mén)</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>
聯(lián)系客服