xmlbeans 2.5.0使用详细说明

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

 

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

 
   
     
       
       
       
       
     

   

 


 
   
     
     
   

   
   
   
 


 
   
     
     
     
     
   

 


 
   
     
     
   

 


 

  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

把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(); // } } }

 

你可能感兴趣的:(xmlbeans 2.5.0使用详细说明)