首页 技术 正文
技术 2022年11月16日
0 收藏 412 点赞 4,323 浏览 5892 个字

由上一遍的准备工作完成后,可以很简单的就进行对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的版本太低导致,假如有读者也遇到问题,可以联系我,讨论一下,找出解决方法。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,954
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,479
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,108
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,740
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,774