xmlBean学习二
由上一遍的准备工作完成后,可以很简单的就进行对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学习二的更多相关文章
- emberjs学习二(ember-data和localstorage_adapter)
emberjs学习二(ember-data和localstorage_adapter) 准备工作 首先我们加入ember-data和ember-localstorage-adapter两个依赖项,使用 ...
- ReactJS入门学习二
ReactJS入门学习二 阅读目录 React的背景和基本原理 理解React.render() 什么是JSX? 为什么要使用JSX? JSX的语法 如何在JSX中如何使用事件 如何在JSX中如何使用 ...
- TweenMax动画库学习(二)
目录 TweenMax动画库学习(一) TweenMax动画库学习(二) TweenMax动画库学习(三) Tw ...
- Hbase深入学习(二) 安装hbase
Hbase深入学习(二) 安装hbase This guidedescribes setup of a standalone hbase instance that uses the local fi ...
- Struts2框架学习(二) Action
Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...
- Python学习二:词典基础详解
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...
- Quartz学习--二 Hello Quartz! 和源码分析
Quartz学习--二 Hello Quartz! 和源码分析 三. Hello Quartz! 我会跟着 第一章 6.2 的图来 进行同步代码编写 简单入门示例: 创建一个新的java普通工程 ...
- SpringCloud学习(二):微服务入门实战项目搭建
一.开始使用Spring Cloud实战微服务 1.SpringCloud是什么? 云计算的解决方案?不是 SpringCloud是一个在SpringBoot的基础上构建的一个快速构建分布式系统的工具 ...
- DjangoRestFramework学习二之序列化组件、视图组件 serializer modelserializer
DjangoRestFramework学习二之序列化组件.视图组件 本节目录 一 序列化组件 二 视图组件 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 序列化组 ...
随机推荐
- uva 11768
// 扩展欧几里得算法 // 先求出一个解 再求出区间 [x1,x2]有几个整数符合条件// 需要注意的是 水平和垂直2种情况的处理 还有正数和负数取整的细微差别#include <iostre ...
- mysql违背了唯一约束
执行一批数据,违背唯一约束时会中断,导致后面的数据写不进去. mysql有提供ignore关键字,使用insert ignore into .... 这样,当违背了唯一约束的时候~就会直接跳过,不会报 ...
- TCP/IP详解学习笔记(4)-ICMP协议,ping和Traceroute
1.IMCP协议介绍 前面讲到了,IP协议并不是一个可靠的协议,它不保证数据被送达,那么,自然的,保证数据送达的工作应该由其他的模块来完成.其中一个重要的模块就是ICMP(网络控制报文)协议. 当传送 ...
- fork/join使用示例
fork/join框架是用多线程的方式实现分治法来解决问题.fork指的是将问题不断地缩小规模,join是指根据子问题的计算结果,得出更高层次的结果. fork/join框架的使用有一定的约束条件: ...
- 《Python CookBook2》 第一章 文本 - 每次处理一个字符 && 字符和字符值之间的转换
文本 - 总结: 什么是文本Python 中的string 类型是不可变类型.文本,一个字符的矩阵,每一个单独的文本快可以被缩进和组织起来. 基本的文本操作①解析数据并将数据放入程序内部的结构中:②将 ...
- rfid门禁系统笔记
非接触式IC卡性能简介 主要指标: 1:容量为8K 位的EEPROM 2:分为16个扇区,每个扇区为4块,每块16个直接,以块为存取单位 3:每个扇区有独立的一组密码和访问控制 4:每张卡具有唯一的序 ...
- LeetCode题解——Integer to Roman
题目: 将整数转换为罗马数字.罗马数字规则可以参考: 维基百科-罗马数字 解法: 类似于进制转换,从大的基数开始,求整数对基数的商和余,来进行转换. 代码: class Solution { publ ...
- 【转】VC6.0附带小工具软件一览
)ActiveX Control Test Container称为"ActiveX 控件测试容器",顾名思义,此工具的主要功能就是测试ActiveX 控件,可以通过改变Active ...
- L0、L1与L2范数、核范数(转)
L0.L1与L2范数.核范数 今天我们聊聊机器学习中出现的非常频繁的问题:过拟合与规则化.我们先简单的来理解下常用的L0.L1.L2和核范数规则化.最后聊下规则化项参数的选择问题.这里因为篇幅比较庞大 ...
- java 拷贝功能
java 中的 拷贝分为浅拷贝 和 深拷贝 浅拷贝需要实现Cloneable接口,深拷贝需要实现Serializable接口. public class Square implements Clone ...