JaxbReadXml文件 package com.jaxb; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; import java.text.MessageFormat; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; import megen.util.jaxb.JaxbContextManager; @SuppressWarnings(" public class JaxbReadXml { @SuppressWarnings({ "unchecked" }) public static T readString(Class clazz, String context) throws JAXBException, IOException { String res = ""; StringBuffer buffer = null; try { FileInputStream fr = new FileInputStream("C:\\test.xml"); buffer = new StringBuffer(); BufferedReader in = new BufferedReader(new InputStreamReader(fr)); buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } res = buffer.toString(); // JAXBContext jc = JAXBContext.newInstance(clazz); // Unmarshaller u = jc.createUnmarshaller(); //加载映射bean类 JAXBContext jaxbContext = JaxbContextManager.getInstance().getJaxbContext( "com.jaxb.TestOrgs"); Unmarshaller um = jaxbContext.createUnmarshaller(); StreamSource streamSource = new StreamSource(new StringReader(buffer.toString())); return (T) um.unmarshal(streamSource); // 返回一个结构 //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; } } @SuppressWarnings("resource") public static void main(String[] args) throws JAXBException, IOException { StringBuffer buffer = null; TestOrgs testOrgs = JaxbReadXml.readString(TestOrgs.class, "src/uktbj70/com/jaxb/test.xml"); //System.out.println(testOrgs.toString()); // System.out.println(testOrgs.getSize()); // System.out.println(testOrgs.getBatchNumber()); // System.out.println(testOrgs.getErrmsg()); // for (TestOrg o : testOrgs) { // System.out.println(o.getOrgName()); // } try { FileInputStream fr = new FileInputStream("src/uktbj70/com/jaxb/test.xml"); buffer = new StringBuffer(); BufferedReader in = new BufferedReader(new InputStreamReader(fr)); buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } System.out.println(buffer.toString()); } catch (FileNotFoundException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } } } TestOrg 文件 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; @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; @SuppressWarnings(" public Long getOrgId() { return orgId; } @SuppressWarnings(" public void setOrgId(Long orgId) { this.orgId = orgId; } @SuppressWarnings(" public Long getParentId() { return parentId; } @SuppressWarnings(" public void setParentId(Long parentId) { this.parentId = parentId; } @SuppressWarnings(" public String getOrgName() { return orgName; } @SuppressWarnings(" public void setOrgName(String orgName) { this.orgName = orgName; } @SuppressWarnings(" public String getOrgCode() { return orgCode; } @SuppressWarnings(" public void setOrgCode(String orgCode) { this.orgCode = orgCode; } @SuppressWarnings(" public String getOrgType() { return orgType; } @SuppressWarnings(" public void setOrgType(String orgType) { this.orgType = orgType; } @SuppressWarnings(" public Date getStartDate() { return startDate; } @SuppressWarnings(" public void setStartDate(Date startDate) { this.startDate = startDate; } @SuppressWarnings(" public Date getEndDate() { return endDate; } @SuppressWarnings(" public void setEndDate(Date endDate) { this.endDate = endDate; } @SuppressWarnings(" public String getAttribute() { return attribute; } @SuppressWarnings(" public void setAttribute(String attribute) { this.attribute = attribute; } @SuppressWarnings(" public Date getInsertTime() { return insertTime; } @SuppressWarnings(" public void setInsertTime(Date insertTime) { this.insertTime = insertTime; } } TestOrgs 文件 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; } } test.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.suzhou</org_name> <org_code>512AAS</org_code> <org_type>deport</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.hunshan</org_name> <org_code>512AAS</org_code> <org_type>deport</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.putian</org_name> <org_code>512AAS</org_code> <org_type>deport</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.nanjing</org_name> <org_code>512AAS</org_code> <org_type>deport</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.fuzhou</org_name> <org_code>512AAS</org_code> <org_type>deport</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>
jaxb解析xml 实例
猜你喜欢
转载自chimpp55.iteye.com/blog/2344399
今日推荐
周排行