JAXB使用

来源:互联网 发布:淘宝卖零食需要证件吗 编辑:程序博客网 时间:2024/06/16 07:59

以前读XML文件一般都是用Dom4j。 今天才知道java1.6以后加入了JAXB(Java Architecture for XML Binding)这个好东西。刚好科研需要用到,写了一个Demo,运行通过了。

首先,要有一个XML文件(test.xml)

<?xml version="1.0" encoding="UTF-8"?><document id="DrugDDI.d390" origId="19-norandrostenedione"><sentence id="DrugDDI.d390.s0" origId="s0" text="No drug, nutritional supplement, food or herb interactions have yet been reported."><entity id="DrugDDI.d390.s0.e0" origId="s0.p0" charOffset="3-7" type="drug" text="drug"/></sentence></document>


然后写个绑定的类(Document.java)

package hilab.textmining.phos.corpora;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;import javax.xml.bind.annotation.XmlType;/** * <p>Java class for anonymous complex type. *  * <p>The following schema fragment specifies the expected content contained within this class. *  * <pre> * <complexType> *   <complexContent> *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> *       <sequence> *         <element ref="{}sentence" maxOccurs="unbounded"/> *       </sequence> *       <attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}string" /> *     </restriction> *   </complexContent> * </complexType> * </pre> *  *  */@XmlAccessorType(XmlAccessType.FIELD)@XmlType(name = "", propOrder = {    "sentence"})@XmlRootElement(name = "document")public class Document {    @XmlElement(required = true)    protected List<Sentence> sentence;    @XmlAttribute(name = "id", required = true)    protected String id;    /**     * Gets the value of the sentence property.     *      * <p>     * This accessor method returns a reference to the live list,     * not a snapshot. Therefore any modification you make to the     * returned list will be present inside the JAXB object.     * This is why there is not a <CODE>set</CODE> method for the sentence property.     *      * <p>     * For example, to add a new item, do as follows:     * <pre>     *    getSentence().add(newItem);     * </pre>     *      *      * <p>     * Objects of the following type(s) are allowed in the list     * {@link Sentence }     *      *      */    public List<Sentence> getSentence() {        if (sentence == null) {            sentence = new ArrayList<Sentence>();        }        return this.sentence;    }    /**     * Gets the value of the id property.     *      * @return     *     possible object is     *     {@link String }     *          */    public String getId() {        return id;    }    /**     * Sets the value of the id property.     *      * @param value     *     allowed object is     *     {@link String }     *          */    public void setId(String value) {        this.id = value;    }}
最后写个测试类(TestJAXB.java)

package hilab.textmining.phos.utils;import java.io.FileNotFoundException;import java.io.FileReader;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Unmarshaller;import hilab.textmining.phos.corpora.Document;public class TestJAXB {public static void main(String[] args) throws FileNotFoundException {    Document corpus;    try {JAXBContext context = JAXBContext.newInstance(Document.class);FileReader fr = new FileReader("test.xml");Unmarshaller um = context.createUnmarshaller();   //写入xml用context.createMarshaller()corpus = (Document) um.unmarshal(fr);System.out.println(corpus.getSentence().get(0).getText());} catch (JAXBException e) {// TODO Auto-generated catch blocke.printStackTrace();}    }}


0 0