由上一遍的准备工作完成后,可以很简单的就进行对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. 添加gif效果图

    1.贴加第三方包 http://blog.csdn.net/iamlazybone/article/details/5972234 2. <FrameLayout android:id=&quo ...

  2. cocos2d-x 2.1.2 bug发现

    1.在做屏蔽触摸时发现 extensions中的CCScrollView类 void CCScrollView::registerWithTouchDispatcher() { CCDirector: ...

  3. UVA 821 Page Hopping 网页跳跃(BFS,简单)

    题意: 给一个无权有向图,可认为边的长度为1,求两点间的平均长度(即所有点对的长度取平均),保留3位小数.保证任意点对都可达. 思路: 简单题.直接穷举每个点,进行BFS求该点到其他点的距离.累加后除 ...

  4. JetBrains优秀工具推荐

    1.JAVA开发工具 IDEA IDEA 全称 IntelliJ IDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的Java开发工具之一,尤其在智能代码助手.代码自动提示.重 ...

  5. 流媒體】jrtplib—VS2010下RTP开源协议库JRTPLIB3.9.1编译

    一.JRTPLIB简介 老外用C++编写的开源RTP协议库,用来进行实时数据传输,可以运行在 Windows.Linux. FreeBSD.Solaris.Unix和VxWorks 等多种操作系统上, ...

  6. 3732 Ahui Writes Word

    // N个物品 放进容量为C的背包里面 要求价值最大// 一看 第一反应是0 1背包 不过 N=100000 C=10000// 注意到 v,c在 10以内// 那么 最多就100种组合了 然后就转化 ...

  7. Oracle 创建和使用视图

    一.what(什么是视图?) 1.视图是一种数据库对象,是从一个或者多个数据表或视图中导出的虚表,视图所对应的数据并不真正地存储在视图中,而是存储在所引用的数据表中,视图的结构和数据是对数据表进行查询 ...

  8. UE 使用技巧

    一.关于正则表达式的使用 删除空行: 替换 %[ ^t]++^p 为 空串 替换回车换行符:替换^p 为 空串 删除行尾空格: 替换 [ ^t]+$ 为 空串 删除行首空格: 替换 %[ ^t]+ 为 ...

  9. lightoj 1022

    直接算即可,特别要注意精度 #include<cstdio> #include<cmath> int main(){ int t, CASE(0); double r; sca ...

  10. Hbase Basic Prerequisites

    Table 2. Java HBase Version   JDK 6      JDK 7       JDK 8 1.0 Not     Supported yes Running with JD ...