1. 程式人生 > >JAVA 將xml Schema 文件轉化成 XML檔案

JAVA 將xml Schema 文件轉化成 XML檔案

  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>