JAXB使用

以前读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 block
			e.printStackTrace();
		}
	    

	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值