- 首先从https://jaxb.java.net/2.2.11/ 获取最新的jaxb 类
- 这里提供一个Schema文档
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xsd:element name="bookstore" type="bookstoreType"/> <xsd:complexType name="bookstoreType"> <xsd:sequence maxOccurs="unbounded"> <xsd:element name="book" type="bookType"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title" type="xsd:string"/> <xsd:element name="author" type="authorName"/> <xsd:element name="price" type="xsd:decimal"/> </xsd:sequence> <xsd:attribute name="genre" type="xsd:string"/> </xsd:complexType> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string"/> <xsd:element name="last-name" type="xsd:string"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
<p>用xjc工具生成java类</p>
-nv 对于输入的模式不执行严格的XML验证
-b <file> 指定外部的绑定文件
-d <dir> 指定生成的文件的存放路径
-p <pkg> 指定目标包
-classpath <arg> 指定classpath
-use-runtime <pkg> impl.runtime包不被生成
-xmlschema 输入的模式是一个W3C XML模式(默认
配置jaxb类包
4) 将步骤3生成的四个JAVA文件导入到你JAVA工程下
由JAVA类得到XML文件
package test;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import core.CatalogType;
public class jaxbWriteXml {
/**
* @param args
* @throws JAXBException
*/
public static void main(String[] args) throws JAXBException {
// TODO Auto-generated method stub
try {
File file=new File("testHomeWork.xml");
JAXBContext jaxbContext=JAXBContext.newInstance(BookstoreType.class);
Marshaller marshaller=jaxbContext.createMarshaller();
//指定编码格式
marshaller.setProperty(Marshaller.JAXB_ENCODING, "gbk");
//是否格式化生成的xml
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
//是否忽略xml头信息
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
BookstoreType bookstoreType=new BookstoreType();
List<BookType> bookTypes=new ArrayList<BookType>();
List<AuthorName> authorNames=new ArrayList<AuthorName>();
//for(int i=0;i<3;i++)
for(int i=0;i<3;i++)
{
AuthorName authorName=new AuthorName();
authorName.setFirstName("张三"+i);
authorName.setLastName("李四"+i);
BookType bookType=new BookType();
bookType.setGenre("IT类");
bookType.setPrice(45.00+i*5);
bookType.setTitle("Java每日一行第"+i+"行");
bookType.author=authorName;
bookTypes.add(bookType);
}
bookstoreType.book=bookTypes;
marshaller.marshal(bookstoreType, new File("testHome.xml"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
结果:
<?xml version="1.0" encoding="gbk" standalone="yes"?>
<bookstoreType>
<book genre="IT类">
<title>Java每日一行第0行</title>
<author>
<first-name>张三0</first-name>
<last-name>李四0</last-name>
</author>
<price>45.0</price>
</book>
<book genre="IT类">
<title>Java每日一行第1行</title>
<author>
<first-name>张三1</first-name>
<last-name>李四1</last-name>
</author>
<price>50.0</price>
</book>
<book genre="IT类">
<title>Java每日一行第2行</title>
<author>
<first-name>张三2</first-name>
<last-name>李四2</last-name>
</author>
<price>55.0</price>
</book>
</bookstoreType>