xmlbeans 生成java_如何利用xmlBeans读、写、修改、删除xml文档?

建test.xsdconfig文件

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

sample.xmlbean

建test.xsd文件

encoding="UTF-8"?>

xmlns:xs="http://www.w3.org/2001/XMLSchema"

elementFormDefault="qualified">

name="Customers">

name="customer"

type="customerType"/>

name="customerType">

type="xs:int"/>

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

type="addressType"/>

name="addressType">

type="primaryAddressType"/>

type="billingAddressType"/>

name="primaryAddressType">

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

name="billingAddressType">

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

 <?xml version="1.0"

encoding="UTF-8"?>

xmlns:xs="http://www.w3.org/2001/XMLSchema"

elementFormDefault="qualified">

name="Customers">

name="customer"

type="customerType"/>

name="customerType">

type="xs:int"/>

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

type="addressType"/>

name="addressType">

type="primaryAddressType"/>

type="billingAddressType"/>

name="primaryAddressType">

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

name="billingAddressType">

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

type="xs:string"/>

运行代码:

scomp -src test\src -out

build\testXmlBean.jar D:\xmlbeans-2.4.0\test\test.xsd -compiler

D:\myeclipse\common\binary\com.sun.java.jdk.win32.x86_1.6.0.013\bin\javac

D:\xmlbeans-2.4.0\test\test.xsdconfig

生成java包testXmlBean.jar

,在build目录下,并生成源代码在test\src目录下面。

新建一个Java

Project——testXMLBeans,并将D:\xmlbeans-2.4.0\lib下的jar文件以及刚才我们生成的testXmlBean.jar加入到testXMLBeans。

新建一个Java Class:xmlBeans.java 源码如下:

import java.io.File;

import sample.xmlbean.*;

import org.apache.xmlbeans.XmlOptions;

public class xmlBeans {

private String filename = null;

public xmlBeans(String filename) {

super();

this.filename = filename;

}

public void Reader() {//实现读操作

try {

File xmlFile = new File(filename);

CustomersDocument doc =

CustomersDocument.Factory.parse(xmlFile);

CustomerType[] customers =

doc.getCustomers().getCustomerArray();

for (int i = 0; i < customers.length; i++) {

CustomerType customer = customers[i];

println("Customer#" + i);

println("Customer ID:" + customer.getId());

println("First name:" + customer.getFirstname());

println("Last name:" + customer.getLastname());

println("Gender:" + customer.getGender());

println("PhoneNumber:" + customer.getPhoneNumber());

// Primary address

PrimaryAddressType primaryAddress = customer.getAddress()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值