JAVA 将xml Schema 文档转化成 XML文件

1 篇文章 0 订阅
1 篇文章 0 订阅

  1.  首先从https://jaxb.java.net/2.2.11/ 获取最新的jaxb 类
  2. 这里提供一个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> 
  3. <p>用xjc工具生成java类</p>
xjc工具基于此模式来绑定一个模式到Java类,xjc命令行接口的一些选项列在下表:
-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>





  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值