由上一遍的准备工作完成后,可以很简单的就进行对xml文件的操作,

package com;

import java.io.File;
import java.io.IOException; import org.apache.xmlbeans.XmlException; import sample.xmlbean.AddressType;
import sample.xmlbean.BillingAddressType;
import sample.xmlbean.CustomerType;
import sample.xmlbean.CustomersDocument;
import sample.xmlbean.CustomersDocument.Customers;
import sample.xmlbean.PrimaryAddressType; public class CustomerXMLBean { private String fileName=null;
public CustomerXMLBean(String fileName){
this.fileName=fileName;
}
/**
* 读取xml文件
*/
public void customerReader(){
File file = new File(fileName);
try {
CustomersDocument document = CustomersDocument.Factory.parse(file);
Customers customers = document.getCustomers();
//System.out.println(customers);
CustomerType[] types = document.getCustomers().getCustomerArray();
for(CustomerType customer:types){
System.out.println(customer.getId());
System.out.println(customer.getGender());
System.out.println(customer.getFirstname()+" "+customer.getLastname());
System.out.println(customer.getPhoneNumber());
AddressType address = customer.getAddress();
//System.out.println(address);
PrimaryAddressType primaryAddress = address.getPrimaryAddress();
BillingAddressType billingAddress = address.getBillingAddress();
System.out.println("---------primaryAddress----------");
System.out.println(primaryAddress.getAddressLine1());
System.out.println(primaryAddress.getAddressLine2());
System.out.println(primaryAddress.getPostalCode());
System.out.println("---------billingAddress----------");
System.out.println(billingAddress.getReceiver());
System.out.println(billingAddress.getPostalCode());
System.out.println(billingAddress.getAddressLine1());
}
} catch (XmlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 新建一个xml文件
* @param args
*/ public void createCustomer(){
/*//创建document
CustomersDocument doc = CustomersDocument.Factory.newInstance();
//添加customer
CustomerType customer = doc.addNewCustomers().addNewCustomer();
customer.setId(3);
customer.setGender("female");
customer.setFirstname("zhang");
customer.setLastname("san");
customer.setPhoneNumber("123456789");
//添加address
AddressType address = customer.addNewAddress();
//添加新的BillingAdress
BillingAddressType billingAddress = address.addNewBillingAddress();
billingAddress.setReceiver("lisi");
billingAddress.setPostalCode("53600");
billingAddress.setAddressLine1("guohui");
billingAddress.setAddressLine2("E5");
//添加新的PrimaryAddress
PrimaryAddressType primaryAddress = address.addNewPrimaryAddress();
primaryAddress.setPostalCode("23645456");
primaryAddress.setAddressLine1("wanshoulu");
primaryAddress.setAddressLine2("302");
File file = new File(fileName);
try {
doc.save(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
try{
// Create Document<br />
CustomersDocument doc = CustomersDocument.Factory.newInstance();
// Add new customer<br />
CustomerType customer = doc.addNewCustomers().addNewCustomer();
// set customer info<br />
customer.setId(3);
customer.setFirstname("Jessica");
customer.setLastname("Lim");
customer.setGender("female");
customer.setPhoneNumber("1234567"); // Add new address<br />
AddressType address = customer.addNewAddress(); // Add new<br />
// PrimaryAddress<br />
PrimaryAddressType primaryAddress = address.addNewPrimaryAddress();
primaryAddress.setPostalCode("350106");
primaryAddress.setAddressLine1("#25-1");
primaryAddress.setAddressLine2("SHINSAYAMA 2-CHOME"); // Add new<br />
// BillingAddress<br />
BillingAddressType billingAddress = address.addNewBillingAddress();
billingAddress.setReceiver("Ms Danielle");
billingAddress.setPostalCode("350107");
billingAddress.setAddressLine1("#167");
billingAddress.setAddressLine2("NORTH TOWER HARBOUR CITY");
File xmlFile = new File(fileName);
doc.save(xmlFile);
} catch (Exception ex){
ex.printStackTrace();
}
}
/**
* 添加一个customer节点
*/
public void addNewCustomer() {
try {
File xmlFile = new File(fileName);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType customer = doc.getCustomers().addNewCustomer();
System.out.println(customer.documentProperties()+"~~~~~~~~~");
customer.setId(3);
customer.setGender("female");
customer.setFirstname("zhang");
customer.setLastname("san");
customer.setPhoneNumber("123456789");
//添加address
AddressType address = customer.addNewAddress();
//添加新的BillingAdress
BillingAddressType billingAddress = address.addNewBillingAddress();
billingAddress.setReceiver("lisi");
billingAddress.setPostalCode("53600");
billingAddress.setAddressLine1("guohui");
billingAddress.setAddressLine2("E5");
//添加新的PrimaryAddress
PrimaryAddressType primaryAddress = address.addNewPrimaryAddress();
primaryAddress.setPostalCode("23645456");
primaryAddress.setAddressLine1("wanshoulu");
primaryAddress.setAddressLine2("302");
//保存
doc.save(xmlFile);
} catch (Exception ex) {
ex.printStackTrace();
}
} /**
* 删除一个节点
* @param args
*/
public void deleteCustomer(){
File file = new File(fileName);
try {
CustomersDocument doc = CustomersDocument.Factory.parse(file);
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (int i = 0; i < customers.length; i++) {
CustomerType customer = customers[i];
if(customer.getId()==3){
customer.setNil() ;
break;
}
}
doc.save(file);
} catch (XmlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static void main(String[] args) {
// TODO Auto-generated method stub
CustomerXMLBean b = new CustomerXMLBean("E:\\code\\workspace\\XmlBean\\customers.xml");
CustomerXMLBean b1 = new CustomerXMLBean("E:\\code\\workspace\\XmlBean\\customers1.xml");
b1.createCustomer();
//b.addNewCustomer();
b.deleteCustomer();
b1.customerReader();
b.customerReader(); }

以上代码除了删除的以外都可以运行出来,唯独删除,出错,报了以下异常:

Exception in thread "main" org.apache.xmlbeans.impl.values.XmlValueNotNillableException
at org.apache.xmlbeans.impl.values.XmlObjectBase.setNil(XmlObjectBase.java:624)
at com.CustomerXMLBean.deleteCustomer(CustomerXMLBean.java:167)
at com.CustomerXMLBean.main(CustomerXMLBean.java:187)

经过我反编译通过上篇使用scomp生成的jar包,发现接口中定义的setNil()方法并没有被实现重写,暂时没有想出问题出在哪,或许可能因为使用的xmlBean的版本太低导致,假如有读者也遇到问题,可以联系我,讨论一下,找出解决方法。

xmlBean学习二的更多相关文章

  1. emberjs学习二(ember-data和localstorage_adapter)

    emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...

  2. ReactJS入门学习二

    ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...

  3. TweenMax动画库学习(二)

    目录            TweenMax动画库学习(一)            TweenMax动画库学习(二)            TweenMax动画库学习(三)            Tw ...

  4. Hbase深入学习(二) 安装hbase

    Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...

  5. Struts2框架学习(二) Action

    Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...

  6. Python学习二:词典基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...

  7. Quartz学习--二 Hello Quartz! 和源码分析

    Quartz学习--二  Hello Quartz! 和源码分析 三.  Hello Quartz! 我会跟着 第一章 6.2 的图来 进行同步代码编写 简单入门示例: 创建一个新的java普通工程 ...

  8. SpringCloud学习(二):微服务入门实战项目搭建

    一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...

  9. DjangoRestFramework学习二之序列化组件、视图组件 serializer modelserializer

      DjangoRestFramework学习二之序列化组件.视图组件   本节目录 一 序列化组件 二 视图组件 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 序列化组 ...

随机推荐

  1. C#进程启动程序,并禁止原窗口操作

    Process myProcess = new Process();            myProcess.StartInfo.FileName = exeName;            myP ...

  2. 【C#学习笔记】窗口隐藏、最小化、最大化、正常化

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. liux vim命令

    命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...

  4. 开通GitHub以及使用笔记

    把小游戏的代码和博客迁移到GitHub上,路径是:https://github.com/GAMTEQ,欢迎访问 以下是使用GITHUB的一些命令 504  cd code 506  mkdir Fai ...

  5. 【原】Storm学习资料推荐

    4.Storm学习资料推荐 书籍: 英文: Learning Storm: Ankit Jain, Anand Nalya: 9781783981328: Amazon.com: Books Gett ...

  6. oc_转_构造对象的方法,以及类的继承

    一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情: 1) 使用alloc方法来分配存储空间 ...

  7. Javascript手记-执行环境和作用域

    执行环境是javascript一个重要的概念,执行环境定义了变量有权访问其他数据决定了他们各自的行为,每个执行环境 都有一个与之关联的变量,环境中定义的所有变量和函数都保存在这个对象中,虽然我们编写的 ...

  8. hadoop的压缩解压缩,reduce端join,map端join

    hadoop的压缩解压缩 hadoop对于常见的几种压缩算法对于我们的mapreduce都是内置支持,不需要我们关心.经过map之后,数据会产生输出经过shuffle,这个时候的shuffle过程特别 ...

  9. 在已创建的DataTable对象中添加在首列一列

    问题描述: 从数据库读取出来的表数据赋给到了DataTable上,将DataTable中数据显示到DataGridView中时希望在DataGridView的第一列显示一列. 解决方法: DataTa ...

  10. lipo命令

    工作中,xcode工程遇到一个bug file was built for archive which is not the architecture being linked armv7 找了一些资 ...