xmlbeans-2.4.0 生成JAVA代码BAT文件

首先要设置xmlbeans的环境变量

 

path=$xmlbean_home/bin

 

批处理文件目录结构如下

 

├─build
└─schema

       ├────config_zh.xsd

       ├────config_zh.xsdconfig

────────────config.bat

config.bat >

scomp -src build\src  -out build\configXmlBean.jar schema\config_zh.xsd  schema\config_zh.xsdconfig

 config_zh.xsdconfig>

<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">

  <xb:namespace>
    <xb:package>com.huawei.xml.xmlbeans.config</xb:package>
  </xb:namespace>

</xb:config>

 

 JAVA代码 参考

 

http://beyond429.iteye.com/blog/277701

 

public static String generateXML() throws IOException, XmlException{
       CustomersDocument doc=CustomersDocument.Factory.newInstance();
       CustomerType cust=doc.addNewCustomers().addNewCustomer();
       cust.setId(1000);
       cust.setGender("female");
       cust.setFirstname("");
       cust.setLastname( "Lee" ); 
       cust.setPhoneNumber( "123456" ); 
       
       AddressType address=cust.addNewAddress(); 
        PrimaryAddressType pAddress=address.addNewPrimaryAddress(); 
       pAddress.setPostalCode( "100000" ); 
       pAddress.setAddressLine1( "ABC" ); 
       pAddress.setAddressLine2( "abc" ); 
       BillingAddressType bAddress=address.addNewBillingAddress(); 
       bAddress.setAddressLine1( "200000" ); 
       bAddress.setAddressLine1( "XYZ" ); 
       bAddress.setAddressLine2( "xyz" ); 
       String xmlDoc=doc.toString(); 
       System. out .println( "-------------------------------------" ); 
       System. out .println(xmlDoc); 
       System. out .println( "--------------------------------------" ); 
       return xmlDoc; 
    } 

 

 

 public static void readXML(String xmlDoc) throws XmlException, IOException
    {
        CustomersDocument doc = CustomersDocument.Factory.parse(xmlDoc);
        CustomerType[] custs = doc.getCustomers().getCustomerArray();
        for (int i = 0; i < custs.length; i++)
        {
            CustomerType cust = (CustomerType) custs[i];
            System.out.println("cust#" + i);
            System.out.println("custId:" + cust.getId());
            cust.setLastname("Lee");
            cust.setPhoneNumber("123456");
            AddressType address = cust.addNewAddress();
            PrimaryAddressType pAddress = address.addNewPrimaryAddress();
            pAddress.setPostalCode("100000");
            pAddress.setAddressLine1("ABC");
            pAddress.setAddressLine2("abc");
            BillingAddressType bAddress = address.addNewBillingAddress();
            bAddress.setAddressLine1("200000");
            bAddress.setAddressLine1("XYZ");
            bAddress.setAddressLine2("xyz");
            String xmlDoc = doc.toString();
            System.out.println("-------------------------------------");
            System.out.println(xmlDoc);
            System.out.println("--------------------------------------");
            return xmlDoc;
        }
    }

 

你可能感兴趣的:(java,apache,xml,Blog)