- 浏览: 196865 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (75)
- JAVA (16)
- Eclipse (1)
- Spring (3)
- Oracle (12)
- gwt (3)
- html (6)
- 数据库 (8)
- Linux (9)
- javascript (3)
- xml (1)
- ubuntu (3)
- apache (1)
- tomcat (1)
- 集群 (2)
- 负载均衡 (2)
- nginx (5)
- android (2)
- phonegap (1)
- subversion (1)
- Memcached (1)
- Hadoop (1)
- vi (1)
- IOS Xcode (1)
- 数字签名 (1)
- shell (3)
- 浏览器 (1)
- firefox (1)
- mysql (5)
- couchbase (1)
- ssh (1)
- Jira (1)
- confluence (1)
- wiki (1)
- wget (1)
- GoAccess (1)
- git (1)
- sonar (1)
最新评论
-
793059909:
这种栈信息,可能是什么原因造成的:3XMTHREADINFO ...
怎样使用jstack诊断Java应用程序故障 -
kingcs:
Oracle 密码180天过期,修改为不限 -
di1984HIT:
不错啊。续约西了
使用JDK工具检查运行系统是否存在内存泄露 -
itzhangyang:
lz是不是可以说一下怎么解决的groovy并发的问题啊
怎样使用jstack诊断Java应用程序故障 -
crane.ding:
Web应用集群大体有两种方式,一种是将Session数 ...
Apache与Tomcat搭建集群
大家经常都会遇到xml格式文档的开发,对于解析XML和封装XML格式,我们自然而然的会想到JAXB,JAXB允许以XML格式存储和读取数据,而不需要程序的类结构实现特定的读取XML和保存XML的代码。
以下是需要输出的XML文本格式
首先我们需要使用XML Schema来描述XML格式,怎样自动生成xsd,我们可以通过trang.jar这个包来生成
生成的XSD文件格式如下
现在可以使用jdk自带的xjc命令来生成代码了,xjc的具体使用方面就不多说,大家可以自己看看它的帮助文档
下面我们就可以开始来写代码了
以下是需要输出的XML文本格式
<Books> <Book> <name>书名A</name> <author>作者A</author> <price>作者A</price> </Book> <Book> <name>书名B</name> <author>作者B</author> <price>作者B</price> </Book> </Books>
首先我们需要使用XML Schema来描述XML格式,怎样自动生成xsd,我们可以通过trang.jar这个包来生成
java -jar trang.jar a.xml a.xsd
生成的XSD文件格式如下
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="Books"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" ref="Book"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="Book"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="author"/> <xs:element ref="price"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="name" type="xs:NCName"/> <xs:element name="author" type="xs:NCName"/> <xs:element name="price" type="xs:NCName"/> </xs:schema>
现在可以使用jdk自带的xjc命令来生成代码了,xjc的具体使用方面就不多说,大家可以自己看看它的帮助文档
$ xjc a.xsd parsing a schema... compiling a schema... generated\Book.java generated\Books.java generated\ObjectFactory.java
package generated; 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.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="{}Book" maxOccurs="unbounded"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "book" }) @XmlRootElement(name = "Books") public class Books { @XmlElement(name = "Book", required = true) protected List<Book> book; /** * Gets the value of the book 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 book property. * * <p> * For example, to add a new item, do as follows: * <pre> * getBook().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link Book } * * */ public List<Book> getBook() { if (book == null) { book = new ArrayList<Book>(); } return this.book; } }
package generated; 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.XmlSchemaType; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; /** * <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="{}name"/> * <element ref="{}author"/> * <element ref="{}price"/> * </sequence> * </restriction> * </complexContent> * </complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "name", "author", "price" }) @XmlRootElement(name = "Book") public class Book { @XmlElement(required = true) @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "NCName") protected String name; @XmlElement(required = true) @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "NCName") protected String author; @XmlElement(required = true) @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "NCName") protected String price; /** * 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 author property. * * @return * possible object is * {@link String } * */ public String getAuthor() { return author; } /** * Sets the value of the author property. * * @param value * allowed object is * {@link String } * */ public void setAuthor(String value) { this.author = value; } /** * Gets the value of the price property. * * @return * possible object is * {@link String } * */ public String getPrice() { return price; } /** * Sets the value of the price property. * * @param value * allowed object is * {@link String } * */ public void setPrice(String value) { this.price = value; } }
package generated; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlElementDecl; import javax.xml.bind.annotation.XmlRegistry; import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import javax.xml.namespace.QName; /** * This object contains factory methods for each * Java content interface and Java element interface * generated in the generated 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 { private final static QName _Author_QNAME = new QName("", "author"); private final static QName _Price_QNAME = new QName("", "price"); private final static QName _Name_QNAME = new QName("", "name"); /** * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated * */ public ObjectFactory() { } /** * Create an instance of {@link Book } * */ public Book createBook() { return new Book(); } /** * Create an instance of {@link Books } * */ public Books createBooks() { return new Books(); } /** * Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}} * */ @XmlElementDecl(namespace = "", name = "author") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) public JAXBElement<String> createAuthor(String value) { return new JAXBElement<String>(_Author_QNAME, String.class, null, value); } /** * Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}} * */ @XmlElementDecl(namespace = "", name = "price") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) public JAXBElement<String> createPrice(String value) { return new JAXBElement<String>(_Price_QNAME, String.class, null, value); } /** * Create an instance of {@link JAXBElement }{@code <}{@link String }{@code >}} * */ @XmlElementDecl(namespace = "", name = "name") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) public JAXBElement<String> createName(String value) { return new JAXBElement<String>(_Name_QNAME, String.class, null, value); } }
下面我们就可以开始来写代码了
package generated; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStream; import java.util.List; import javax.xml.bind.JAXB; public class Test { public static void main(String[] args) throws FileNotFoundException { marshal(new FileOutputStream("out.xml")); unmarshal(new FileReader("out.xml")); } private static void unmarshal(FileReader reader) { try { Books books = JAXB.unmarshal(reader, Books.class); System.out.println(books.getBook().size()); System.out.println(books.getBook().get(0).getName()); System.out.println(books.getBook().get(0).getAuthor()); System.out.println(books.getBook().get(0).getPrice()); } finally { try { reader.close(); } catch (IOException e) { } } } private static void marshal(OutputStream output) { ObjectFactory factory = new ObjectFactory(); Books books = factory.createBooks(); List<Book> bookList = books.getBook(); Book book = factory.createBook(); book.setName("书名A"); book.setAuthor("作者A"); book.setPrice("价格A"); bookList.add(book); book = factory.createBook(); book.setName("书名B"); book.setAuthor("作者B"); book.setPrice("价格B"); bookList.add(book); book = factory.createBook(); book.setName("书名C"); book.setAuthor("作者C"); book.setPrice("价格C"); bookList.add(book); try { JAXB.marshal(books, output); } finally { try { output.close(); } catch (IOException e) { } } } }
- trang.jar (693.6 KB)
- 下载次数: 585
发表评论
-
Servlet 3.0 新特性详解
2015-06-07 09:30 951Servlet 3.0 新特性概述 Servlet 3.0 ... -
Android手机应用apk的反编译
2013-12-12 14:15 1337要对Android手机应用apk进行反编译,我 ... -
PhoneGap android loadUrl远程URL
2012-05-07 22:26 3690phoneGap例子中默认是通过super.loadUrl(& ... -
怎样使用jstack诊断Java应用程序故障
2011-03-21 00:21 36194最近一段时间,我们的生产系统升级频繁出现故障, ... -
使用JDK工具检查运行系统是否存在内存泄露
2010-07-18 13:53 6721几个月前老大给我们培训了怎么样使用jmap和jh ... -
运营系统升级故障排查
2010-07-16 17:51 1125公司的运营管理系统是用SSH开发,经过考虑目前新开 ... -
一道简单的面试题
2010-04-04 12:46 3326前几天有位朋友跟我聊天说,最近他去面试遇到一个面试题,叫 ... -
SQL注入
2010-04-03 16:54 1150简单的SQL注入,往往会给应用程序造成严重的问题。最长 ... -
浮点数运算的陷阱
2010-04-03 15:17 1487浮点数的运算不能说是精确的,因为某些数字不能准确表示为 ... -
System.getProperties确定当前的系统属性的参数大全
2009-03-28 13:47 1510getProperties public static Pr ... -
Java代码实现设置系统时间
2009-03-26 22:41 11598在做终端项目中,今天的任务是将服务器返回的系统时间 ... -
使用Ant进行ssh和scp操作
2009-02-18 22:43 3560Ant真的很强大,通过Ant可以进行ssh和scp操作 ... -
文件上传的使用技巧
2008-11-16 17:07 1576相信大家都文件上传比不陌生,也有许多不同的处理方法。文 ... -
反射实体Bean 拷贝实体Bean数据
2008-08-20 22:05 2549public class MyMethod { p ... -
JAVA EXCEL API的使用
2008-08-20 14:14 1665使用Java Excel API生成Excel,以下是做了一个 ...
相关推荐
本人在公司用到了jaxb技术,这是在公司的学习文档,今天跟高兴个大家分享。
本人总结的jaxb详细开发步骤,结合注解
Jabx2 开发指南 xml 转换、序列化xml
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
jaxb-api jaxb-impl jar
JAXB的安装包及插件
Jaxb annotation 使用 Jaxb annotation初步使用
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
解释并解决java开发中使用jaxb解析或组装xml遇到乱码的问题。
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
trang.jar
activation.jar jaxb1-impl.jar jaxb-api.jar jaxb-impl.jar jaxb-xjc.jar jsr173_1.0_api.jar系列jar下载
使用jaxb生成XML例子,含有例子和注解解析
一个JAXB2的基础教程,教你快速运用JAXB通过XSD绑定XML和读写XML
jaxb-2.2.jar jaxb jax
有关Maven项目中缺少jaxb-api的异常报错解决,jaxb-api-2.3.0.jar
jaxb-api jaxb-impl jaxb-xjc jaxws-rt 这四个文件对应的jar包