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
- JAXB 使用
- JAXB使用
- jaxb使用
- JAXB使用
- jaxb 使用 之 marshal
- jaxb 使用之 unmarshall
- JAXB的使用
- JAXB注解使用
- Jaxb annotation使用
- Jaxb annotation初步使用
- Jaxb annotation初步使用
- Java使用jaxb
- Jaxb annotation使用
- JAXB使用经验总结
- Jaxb annotation初步使用
- JAXB 简单使用
- JAXB的使用
- 使用JAXB创建XML
- 提取字符串中的数字
- POJ1330 Nearest Common Ancestors【最近公共祖先】【Tarjan-LCA算法】
- dedecms采集图片本地化代码
- IAR中出现 Failed to write memory at 0x866E: SWIM PROG error [42008]: Attempt to write to protected area
- unity3d屏幕适配
- JAXB使用
- Map 根据value 排序
- 解决JAVA写mysql时出现中文乱码的现象
- [leetcode 110] Balanced Binary Tree
- J2ee的13种规范
- WCF入门
- CSS3动画产生圆圈由小变大向外扩散的效果
- codevs1029 遍历问题(一直前序和后序求中序个数)
- PHP连接MongoDB示例