您的位置:首页 > 其它

xmlbeans 2.5.0使用详细说明

2016-07-29 00:00 465 查看
xmlbeans有点和hibernate相识,hibernate对数据进行对象化操作,xmlbeans对xml进行对象操作化,很好用,不过很少人用,也可能是对初学者来说比较难入门的原因

先到官网上下载xmlbeans 2.5.0,到apache官网下载,下载只要下载xmlbeans-2.5.0.zip就好

解压,可以看到以下的几个目录:

-bin 常用的命令在这个目录

-lib 一些核心或者用到的JAR

-docs 文档

-samples 示例包括schema和生成后的xml

-schemas 一些schema示例,直接可以拿来打包

注意的地方

schema手写的时候最好在IDE里面写,里面可以告诉你详细的错误信息

从里面拿个示例出来,schemas/easypo.xsd

<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:po="http://openuri.org/easypo"
targetNamespace="http://openuri.org/easypo"
elementFormDefault="qualified">

<xs:element name="purchase-order">
<xs:complexType>
<xs:sequence>
<xs:element name="customer" type="po:customer"/>
<xs:element name="date" type="xs:dateTime"/>
<xs:element name="line-item" type="po:line-item" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="shipper" type="po:shipper" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="customer">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
</xs:sequence>
<xs:attribute name="age" type="xs:int"/>
<xs:attribute name="moo" type="xs:int" default="100"/>
<xs:attribute name="poo" type="xs:int" fixed="200"/>
</xs:complexType>

<xs:complexType name="line-item">
<xs:sequence>
<xs:element name="description" type="xs:string"/>
<xs:element name="per-unit-ounces" type="xs:decimal"/>
<xs:element name="price" type="xs:decimal"/>
<xs:element name="quantity" type="xs:integer"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="shipper">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="per-ounce-rate" type="xs:decimal"/>
</xs:sequence>
</xs:complexType>

</xs:schema>

xmlns:po="http://openuri.org/easypo" //下面的元素引用到的都要以po:属性名命名,也可以写成 xmlns="http://openuri.org/easypo" 则下面的引用元素就不能以po:属性名命名

targetNamespace="http://openuri.org/easypo"//这个是打包的时候的明白规则,可以不用,不用的时候可以在打包的时候加上,通过文件.xsdconfig索引定义的包名,下面有详细介绍

注意:如果这个类似这个schema,生成的xml在子节点位置要有attribute,则在增加,修改的方法不能直接用节点的documentsave,要把它转为String后再通过IO保存该XML数据

打包

schema写好后,到bin下打包,把easypo.xsd和easypo.xsdconfig一起扔到bin下

运行cmd窗口,切换到bin的当前目录,运行命令:

scomp -src easyposrc easypo.xsd easypo.xsdconfig

命令解析:

//-src是生成的src的文件名,该src的文件名为easyposrc

easypo.xsd是要打包的schema

easypo.xsdconfig是命名规则,如果你在schema定义了targetNamespace,那么加上这个easypo.xsdconfig没效,最后生成的xml

文件是以targetNamespace定义的参数为准

下面的是一个写好的示例,注意,这个是另外的一个schema

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Customers">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="customer" type="customerType" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="customerType">
<xs:sequence>
<xs:element name="id" type="xs:int" />
<xs:element name="gender" type="xs:string" />
<xs:element name="firstname" type="xs:string" />
<xs:element name="lastname" type="xs:string" />
<xs:element name="phoneNumber" type="xs:string" />
<xs:element name="address" type="addressType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="addressType">
<xs:sequence>
<xs:element name="primaryAddress" type="primaryAddressType" />
<xs:element name="billingAddress" type="billingAddressType" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="primaryAddressType">
<xs:sequence>
<xs:element name="postalCode" type="xs:string" />
<xs:element name="addressLine1" type="xs:string" />
<xs:element name="addressLine2" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="billingAddressType">
<xs:sequence>
<xs:element name="receiver" type="xs:string" />
<xs:element name="postalCode" type="xs:string" />
<xs:element name="addressLine1" type="xs:string" />
<xs:element name="addressLine2" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>


操作xml

把jar放到项目下,引入后,其他的操作都和操作对象没什么多大的区别

public class CustomerTest {
// 增加一个顾客
public boolean addCustomer() throws IOException {
boolean bol = false;
CustomersDocument document = CustomersDocument.Factory.newInstance();
document.addNewCustomers().addNewCustomer();
// Create Document
CustomersDocument doc = CustomersDocument.Factory.newInstance();
// Add new customer
CustomerType customer = doc.addNewCustomers().addNewCustomer();
// set customer info
customer.setId(3);
customer.setFirstname("Jessica");
customer.setLastname("Lim");
customer.setGender("female");
customer.setPhoneNumber("1234567");
// Add new address
AddressType address = customer.addNewAddress();
// Add new PrimaryAddress
PrimaryAddressType primaryAddress = address.addNewPrimaryAddress();
primaryAddress.setPostalCode("350106");
primaryAddress.setAddressLine1("25-1");
primaryAddress.setAddressLine2("SHINSAYAMA 2-CHOME");
// Add new BillingAddress
BillingAddressType billingAddress = address.addNewBillingAddress();
billingAddress.setReceiver("Ms Danielle");
billingAddress.setPostalCode("350107");
billingAddress.setAddressLine1("167");
billingAddress.setAddressLine2("NORTH TOWER HARBOUR CITY");
File xmlFile = new File("c:/1.xml");
doc.save(xmlFile);
bol = true;
return bol;
}
// 修改一个顾客
public boolean selectCustomer(String xmlpath) throws IOException, XmlException {
boolean bol = false;
CustomersDocument doc = CustomersDocument.Factory.parse(new File(xmlpath));
CustomerType[] customersTypes = doc.getCustomers().getCustomerArray();
for (int i = 0; i < customersTypes.length; i++) {
CustomerType customerType = (CustomerType) customersTypes[i];
System.out.println("Customer" + i);
System.out.println("Customer ID:" + customerType.getId());
System.out.println("First name:" + customerType.getFirstname());
System.out.println("Last name:" + customerType.getLastname());
System.out.println("Gender:" + customerType.getGender());
System.out.println("PhoneNumber:"+ customerType.getPhoneNumber());
// Primary address
PrimaryAddressType primaryAddress = customerType.getAddress().getPrimaryAddress();
System.out.println("PrimaryAddress:");
System.out.println("PostalCode:"+ primaryAddress.getPostalCode());
System.out.println("AddressLine1:"+ primaryAddress.getAddressLine1());
System.out.println("AddressLine2:"+ primaryAddress.getAddressLine2());
// Billing address
BillingAddressType billingAddress = customerType.getAddress().getBillingAddress();
System.out.println("BillingAddress:");
System.out.println("Receiver:" + billingAddress.getReceiver());
System.out.println("PostalCode:"+ billingAddress.getPostalCode());
System.out.println("AddressLine1:"+ billingAddress.getAddressLine1());
System.out.println("AddressLine2:"+ billingAddress.getAddressLine2());
}
bol = true;
return bol;
}

// 修改一个顾客
public boolean modifyCustomer(int id,String xmlpath,String lastname) throws IOException, XmlException {
boolean bol = false;
CustomersDocument doc = null;
doc = CustomersDocument.Factory.parse(new File(xmlpath));
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (int i = 0; i < customers.length; i++) {
CustomerType customer = customers[i];
if(customer.getId()==id){
customer.setLastname(lastname);
break;
}
}
doc.save(new File(xmlpath));
bol = true;
return bol;
}
// 删除一个顾客
public boolean deleteCustomer(int id,String xmlpath) throws IOException, XmlException {
boolean bol = false;
CustomersDocument doc = CustomersDocument.Factory.parse(new File(xmlpath));
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (int i = 0; i < customers.length; i++){
CustomerType customer = customers[i];
if(customer.getId()==id){
customer.setNil();
break;
}
}
doc.save(new File(xmlpath));
bol = true;
return bol;
}
/**
* @param args
* @throws
*/
public static void main(String[] args) {
try {
boolean bol = false;
String xmlpath = "c:/1.xml";
bol = new CustomerTest().addCustomer();//新增一个顾客
//            new CustomerTest().modifyCustomer(3, xmlpath, "last");
//            bol = new CustomerTest().selectCustomer(xmlpath);
////            bol = new CustomerTest().deleteCustomer(3, xmlpath);
System.out.println(bol);
} catch (IOException e) {
e.printStackTrace();
}
//        catch (XmlException e) {
//            e.printStackTrace();
//        }
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: