JAXB解析xml

转载 2015年07月06日 17:39:56

废话不多说,直接上代码

核心类:

复制代码
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>
复制代码

 

jaxb解析生成xml

  • 2017年04月28日 16:20
  • 5KB
  • 下载

JAXB解析XML

  • 2012年05月09日 12:18
  • 5.15MB
  • 下载

jaxb 解析多列数据的xml 映射成list

package com.*.getMap; import javax.xml.bind.annotation.*; import java.util.ArrayList; import java.u...

最新JAXB解析XML教程

  • 2012年08月29日 13:13
  • 4.92MB
  • 下载

jaxb解析xml

  • 2012年08月01日 17:27
  • 74KB
  • 下载

使用Java提供的JAXB生成和解析XML

1.Student类: package test; import java.util.List; import javax.xml.bind.annotation.XmlAttribut...

JAXB解析xml文件转换为POJO

Marshal 和 UnMarshal的过程并不复杂,只需要从JAXBContext中获得Marshaller或Unmarshaller对象,就可以让JAXB帮我们 来进行转换。 1.将 xml 文...

JAXB解析xml

核心类: package com.jaxb; import java.io.File; import java.io.FileInputStream; import java.io.IOExc...

jaxb的实例 java解析xml转成javabean

jaxb的实例 java解析xml转成javabean

JAXB解析xml文件转换为POJO

Marshal 和 UnMarshal的过程并不复杂,只需要从JAXBContext中获得Marshaller或Unmarshaller对象,就可以让JAXB帮我们 来进行转换。 1.将 xml 文...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:JAXB解析xml
举报原因:
原因补充:

(最多只允许输入30个字)