xmlbeans解析XML详细介绍的说明很多,官方也有很多参考,不多废话,主要流程如下:
1. 下载 xmlbean 文件 官方或者直接点击
2. 根据XML生成Schema(XML文件的结构和元素约束文件),可使用工具Altova XMLSpy进行生成.破解版下载地址点我.
3.使用 SPY打开XML,依次选择DTD/Schema->Generate DTD/Schema->W3C Schema->输入名称,得到MRO.xsd文件.
4. 编写.xsdconfig文件.主要作用提供给xmlbean生成命名使用.mewasure.xsdconfig
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config"> <xb:namespace> <xb:package>measure.xmlbean</xb:package> </xb:namespace> </xb:config>
4. 拷贝mro.xsd,原始XML文件,mewasure.xsdconfig文件至xmlbean\bin目录下.记得配置JAVAJDK环境变量
5. 执行scomp -out measureXmlBean.jar MRO.xsdmewasure.xsdconfig
7.java使用方法如下
读:
try { File xmlFile = new File(path); BulkPmMrDataFileDocument doc = BulkPmMrDataFileDocument.Factory.parse(xmlFile); FileHeader customers = doc.getBulkPmMrDataFile().getFileHeader(); ENodeB enb = doc.getBulkPmMrDataFile().getENodeB(); Measurement[] measurement = enb.getMeasurementArray(); for (int i = 0; i < measurement.length; i++) { Measurement customer = measurement[i]; println("id:" + i); println("getSmr ID:" + customer.getSmr()); println("getObjectArray name:" + customer.getObjectArray()[0].getId()); } } catch (Exception ex) { ex.printStackTrace(); }
写:
public void createCustomer() { try { // Create Document CustomersDocument doc = CustomersDocument.Factory.newInstance(); // Add new customer CustomerType customer = doc.addNewCustomers().addNewCustomer(); // set customer info customer.setId(3); // Add new address AddressType address = customer.addNewAddress(); // Add new PrimaryAddress PrimaryAddressType primaryAddress = address.addNewPrimaryAddress(); primaryAddress.setPostalCode("350106"); // Add new BillingAddress BillingAddressType billingAddress = address.addNewBillingAddress(); billingAddress.setReceiver("Ms Danielle"); File xmlFile = new File(filename); doc.save(xmlFile); } catch (Exception ex) { ex.printStackTrace(); } } main . public static void main(String[] args) { String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml"; CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename); customerXMLBean.createCustomer(); }
到此全完已经介绍完成.
关于spy破解简单说下:
1. 屏蔽验证.C:\WINDOWS\system32\drivers\etc\hosts加入.
127.0.0.1 altova.com #XMLspy
127.0.0.1 www.altova.com #XMLspy
127.0.0.1 link.altova.com #XMLspy
2. 安装,复制patch到安装目录.点击应用补丁
3. 启动注册,注册码如下
Name:www.shijieweb.cn Company:shijieweb Key-code:PM92JW3-HJMTA8C-P2H3C53-579CT11-PCCYT91-X8D0LM1-PWY8U42