网络编程
位置:首页>> 网络编程>> XML编程>> 利用XMLBean轻轻松松读写XML(7)

利用XMLBean轻轻松松读写XML(7)

 来源:互联网 发布时间:2008-09-04 11:25:00 

标签:

八、利用XMLBean删除一个customer

再增加一个Method:


           

    public void deleteCustomer(int id) {
     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];
        if(customer.getId()==id){
                        customer.setNil() ;
                        break;
               }
   }
   doc.save(xmlFile);
   } catch (Exception ex) {
        ex.printStackTrace();
        }
   }

main method:

    public static void main(String[] args) {
    String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
                   
    CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
                   
    customerXMLBean.deleteCustomer(3);
    }


运行,我们将会看到客户编号为3的客户的资料已经被删除.

九、查询XML

除了本文在以上讲述的,利用XMLBean能轻轻松松完成XML的读写操作外,结合XPath和XQuery,XMLBean还能完成象SQL查询数据库一样方便地查询XML数据. 关于XML查询以及如何创建XML数据库, 我将在另一篇文章里讨论.

十、结束语

XMLBean能帮助我们轻易读写XML,这将有助于我们降低XML的学习和使用,有了这个基础,开发人员将为学习更多地XML相关技术和Web Services,JMS等其他J2EE技术打下良好地基础.

作者:叶枫(http://blog.matrix.org.cn/page/叶枫)
原文地址:http://www.matrix.org.cn/resource/article/44/44027_XMLBean.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com