javaee5 jaxb 学习
来源:互联网 发布:java 时间加减 编辑:程序博客网 时间:2024/05/17 23:59
下面看看 sun 的 jaxb 2 列子,
可以将 xml schema 的规则 转成 我们的java代码,然后通过 Unmarshaller 来读取 一个xml 实例,这样可以减少我们 解析XML的麻烦。
首先创建一个 xml schema person.xsd
- <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <xsd:element name="Person">
- <xsd:complexType>
- <xsd:sequence>
- <xsd:element name="name" type="xsd:string" />
- <xsd:element name="sex" type="xsd:string" />
- <xsd:element name="age" type="xsd:int" />
- <xsd:element name="tel" type="xsd:string" />
- <xsd:element name="address" type="xsd:string" />
- </xsd:sequence>
- </xsd:complexType>
- </xsd:element>
- </xsd:schema>
一个简单的Person 定义
通过 sun 提供的 xjc 的工具,可以把上面的 schema 转化成 java 代码
jdk 自带了一个,在dos下输入下面命令,, -p 指定生成代码的 包名。
xjc -p com.birds.person person.xsd
主意如果使用 jdk 自带的 xjc 就不需要 ,把 javaee 中jaxb 的包加入到classpath中去,用eclipse 也不需要加入。
jdk 1.6中有 自带的javax.xml.bind 包,
如果使用 javaee 中的 xjc 工具的话 ,要加入下面这些包
jaxb1-impl.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
并且生成对应的代码 需要指定 classpath 使用 com.sun.tools.xjc.XJCTask 来生成。
-----
下面是自动生成对应的2个类的代码
- package com.birds.person;
- import javax.xml.bind.annotation.XmlRegistry;
- /**
- * This object contains factory methods for each
- * Java content interface and Java element interface
- * generated in the com.birds.person package.
- * <p>An ObjectFactory allows you to programatically
- * construct new instances of the Java representation
- * for XML content. The Java representation of XML
- * content can consist of schema derived interfaces
- * and classes representing the binding of schema
- * type definitions, element declarations and model
- * groups. Factory methods for each of these are
- * provided in this class.
- *
- */
- @XmlRegistry
- public class ObjectFactory {
- public ObjectFactory() {
- }
- /**
- * Create an instance of {@link Person }
- *
- */
- public Person createPerson() {
- return new Person();
- }
- }
- package com.birds.person;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- 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 name="name" type="{http://www.w3.org/2001/XMLSchema}string"/>
- * <element name="sex" type="{http://www.w3.org/2001/XMLSchema}string"/>
- * <element name="age" type="{http://www.w3.org/2001/XMLSchema}int"/>
- * <element name="tel" type="{http://www.w3.org/2001/XMLSchema}string"/>
- * <element name="address" type="{http://www.w3.org/2001/XMLSchema}string"/>
- * </sequence>
- * </restriction>
- * </complexContent>
- * </complexType>
- * </pre>
- *
- *
- */
- @XmlAccessorType(XmlAccessType.FIELD)
- @XmlType(name = "", propOrder = {
- "name",
- "sex",
- "age",
- "tel",
- "address"
- })
- @XmlRootElement(name = "Person")
- public class Person {
- @XmlElement(required = true)
- protected String name;
- @XmlElement(required = true)
- protected String sex;
- protected int age;
- @XmlElement(required = true)
- protected String tel;
- @XmlElement(required = true)
- protected String address;
- /**
- * Gets the value of the name property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public String getName() {
- return name;
- }
- /**
- * Sets the value of the name property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void setName(String value) {
- this.name = value;
- }
- /**
- * Gets the value of the sex property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public String getSex() {
- return sex;
- }
- /**
- * Sets the value of the sex property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void setSex(String value) {
- this.sex = value;
- }
- /**
- * Gets the value of the age property.
- *
- */
- public int getAge() {
- return age;
- }
- /**
- * Sets the value of the age property.
- *
- */
- public void setAge(int value) {
- this.age = value;
- }
- /**
- * Gets the value of the tel property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public String getTel() {
- return tel;
- }
- /**
- * Sets the value of the tel property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void setTel(String value) {
- this.tel = value;
- }
- /**
- * Gets the value of the address property.
- *
- * @return
- * possible object is
- * {@link String }
- *
- */
- public String getAddress() {
- return address;
- }
- /**
- * Sets the value of the address property.
- *
- * @param value
- * allowed object is
- * {@link String }
- *
- */
- public void setAddress(String value) {
- this.address = value;
- }
- }
再写一个 person.xml 数据,目的就是方面的读取xml中的数据
- <?xml version="1.0"?>
- <Person>
- <name>birds</name>
- <sex>man</sex>
- <age>32</age>
- <tel>0949855</tel>
- <address>China,Bejing</address>
- </Person>
最后是写个 客户端读取 数据
- JAXBContext jc = JAXBContext.newInstance( "com.birds.person" ); // 从生成代码的地方读取,
- Unmarshaller u = jc.createUnmarshaller();
- Person po =(Person)u.unmarshal(new FileInputStream( "person.xml" ) ); //加载xml数据文件。
- System.out.println(po.getAddress());
- System.out.println(po.getAge());
- javaee5 jaxb 学习
- JavaEE5 Jax-ws 学习
- JavaEE5 Jax-ws 学习
- jaxb学习
- JAXB学习
- Jaxb 学习
- JavaEE5 Jax-ws 学习(Soap webservice)
- jAXB学习入门
- java JAXB 学习
- JAXB学习笔记
- java JAXB 学习
- JAXB初步的学习
- JAXB学习之注解
- JavaEE5 Tutorial_Servlet
- JAXB学习一 (入门)
- JAXB学习三 (验证)
- JAXB
- jaxb
- Oracle 创建自增字段的方法—使用序列方法(1)
- Java中的transient
- XML 命名空间
- 技巧: 双引导 Linux
- flex2-custom-metadata
- javaee5 jaxb 学习
- 覆盖 GNU C 库 ― 轻而易举
- javascript 设定cookie
- SYBASE数据导入技巧
- 开始学习记录
- ActiveX安装不成功,直接创建其对象报错,采用接口机制进行判断.
- 小心:防止数据库数据写覆盖
- Asp.net教程:设计IP地址屏蔽功能
- 嵌入式Linux下使用的UI开发库