2008年7月22日 星期二

利用XMLBean寫XML

官方網站
http://xmlbeans.apache.org/documentation/conInstallGuide.html

首先設定環境變數

Windows Setup

On Windows, do the following:

  1. Right-click My Computer, then click Properties.
  2. On the Advanced tab, click Environment Variables.
  3. Add an XMLBEANS_HOME variable whose value is the XMLBeans location.

    For example, if you downloaded the XMLBeans release to the root of your C drive, this might be c:\xmlbeans-1.0.2.

  4. Edit the PATH variable so that it includes the bin directory of your XMLBeans installation. For the XMLBeans release, you could add %XMLBEANS_HOME%\bin.
  5. Edit the CLASSPATH variable to include the xbean.jar included with XMLBeans.

    This is located in the lib directory. If you built XMLBeans from source, you can also use the JAR file in the build/ar or build/lib directories.

  6. For XMLBeans V2, also include the jsr173_1.0_api.jar (or jsr173_api.jar, whichever one came in your download) to the CLASSPATH variable.

    This is found in the lib (or build/lib if you built XMLBeans from source) directory.

準備XML檔案(customers.xml),內容如下:



1
female
Jessica
Lim
1234567


350106
#25-1
SHINSAYAMA 2-CHOME


Ms Danielle
350107
#167
NORTH TOWER HARBOUR CITY




2
male
David
Bill
808182


319087
1033 WS St.
Tima Road


Mr William
672993
1033 WS St.
Tima Road





準備配置文件(文件customer.xsdconfig) 內容如下


sample.xmlbean

建立XML Schema文件(customer.xsd)內容如下








































利用scomp產生Java Classes ,語法如下

scomp [options] [dirs]* [schemaFile.xsd]* [service.wsdl]* [config.xsdconfig]*

  主要参数说明:

  -src [dir] -- 生成的Java Classes存放目录

  -srconly -- 不编译Java Classes,不产生Jar文件

  -out [jarFileName] -- 生成的Jar文件,缺省是xmltypes.jar

  -compiler -- Java编译器的路径,即Javac的位置

  schemaFile.xsd -- XML Schema文件位置

  config.xsdconfig -- xsdconfig文件的位置, 这个文件主要用来制定生成的Java Class的一些文件名规则和Package的名称,在本文,package是sample.xmlbean


在此用的例子如下:

F:\>scomp -src build\src -out build\customerXmlBean.jar customer.xsd -compiler C:\Program Files\Java\jdk1.6.0_02\bin\javac customer.xsdconfig


將生成的customerXmlBean.jar加入到Project的ClassPath.

新建一各Java Class: CustomerXMLBean. 如下:

package com.sample.reader;

import java.io.File;

import sample.xmlbean.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.xmlbeans.XmlOptions;
public class CustomerXMLBean {
private String filename = null;

public CustomerXMLBean(String filename) {
super();
this.filename = filename;
}

public void customerReader() {
try {
File xmlFile = new File(filename);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType[] customers = doc.getCustomers().getCustomerArray();

for (int i = 0; i < customer =" customers[i];" primaryaddress =" customer.getAddress().getPrimaryAddress();" billingaddress =" customer.getAddress().getBillingAddress();" filename = "F://JavaTest//Eclipse//XMLBean//xml//customers.xml" customerxmlbean =" new">

}

 输出结果

Customer#0
Customer ID:1
First name:Jessica
Last name:Lim
Gender:female
PhoneNumber:1234567
PrimaryAddress:
PostalCode:350106
AddressLine1:#25-1
AddressLine2:SHINSAYAMA 2-CHOME
BillingAddress:
Receiver:Ms Danielle
PostalCode:350107
AddressLine1:#167
AddressLine2:NORTH TOWER HARBOUR CITY

Customer#1
Customer ID:2
First name:David
Last name:Bill
Gender:male
PhoneNumber:808182
PrimaryAddress:
PostalCode:319087
AddressLine1:1033 WS St.
AddressLine2:Tima Road
BillingAddress:
Receiver:Mr William
PostalCode:672993
AddressLine1:1033 WS St.
AddressLine2:Tima Road










參考網址
http://www.2ed.cn/Article-3-19078.shtml

沒有留言: