
	import org.xmldb.api.base.*;
	import org.xmldb.api.modules.*;
    import org.xmldb.api.*;

	public class xupdate {
		
		static Collection col = null;
		static String xupdate=null;
	   @SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception {
	      
	      try {
	     //define the driver to connect to XIndice database      
	    	  String driver = "org.apache.xindice.client.xmldb.DatabaseImpl";
	         Class c = Class.forName(driver);
	         
	         Database database = (Database) c.newInstance();
	         DatabaseManager.registerDatabase(database);
	         // define the path of the collection which contains the persons.xml file
	         col = DatabaseManager.getCollection("xmldb:xindice:///db/Test");
	   //to execute a xupdate request we should always start with xupdate:modification      
	         xupdate=   "<xupdate:modifications version=\"1.0\"" +
             " xmlns:xupdate=\"http://www.xmldb.org/xupdate\"> \n" ;
	        
       
         switch(args[0].charAt(1))
	         {
	         case 'A' : 
	   // in case we want add a person element to our file in database      	          
	    	 xupdate=xupdate+" <xupdate:append select=\"/persons\">\n"+
             "<xupdate:element name=\"person\">\n"+
// this function return the number of persons plus one in xml file persons.xml in database        
      "<xupdate:attribute name=\"id\">"+get_count_persons()+"</xupdate:attribute>\n"+
              "<fname>"+args[2]+"</fname>\n"+
              "<lname>"+args[3]+"</lname>\n"+
              "<age>"+args[4]+"</age>\n"+
              "<email>"+args[5]+"</email>\n"+
              "</xupdate:element>\n"+
             "  </xupdate:append> \n  " ;
	         break;
	         case 'R':
// in this case we try to remove a person element defined with id entered in argument 
	        	 xupdate=xupdate+"<xupdate:remove select=\"//person[@id="+args[2]+"]\" />\n";
	             break;
	             
	         case 'E':
	 // update all nodes of a person element which has an id entered in the arguments  	  
	        	  xupdate=xupdate+
	        	  "<xupdate:update select=\"//person[@id="+args[2]+"]/fname\">\n"+
	              args[3]+"\n"+
	              "</xupdate:update> \n  "+
	              "<xupdate:update select=\"//person[@id="+args[2]+"]/lname\">\n"+
	              args[4]+"\n"+
	              "</xupdate:update> \n  "+
	              "<xupdate:update select=\"//person[@id="+args[2]+"]/age\">\n"+
	              args[5]+"\n"+
	              "</xupdate:update> \n  "+
	              "<xupdate:update select=\"//person[@id="+args[2]+"]/email\">\n"+
	              args[6]+"\n"+
	              "</xupdate:update> \n  " ;
	        	 break;
	         }   
//close the modification node 
	         xupdate=xupdate+" </xupdate:modifications>";

//show the xupdate request executed 
	         System.out.println(xupdate);
	            XUpdateQueryService service =
	               (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
	         //update the file in database with xupdate request      
	            service.update(xupdate);
	            
	      }
	      catch (XMLDBException e) {
	         System.err.println("XML:DB Exception occurred " + e.errorCode+","+e.getMessage());
	      }
	      finally {
	          if (col != null) { col.close();}
	      }
	   }
	   public static int get_count_persons()
	   {
//xpath expression which select the root element	
	   String xpath = "//person";
	       int i=0;
	       
	       try {
	       XPathQueryService service =
	            (XPathQueryService) col.getService("XPathQueryService", "1.0");
	         ResourceSet resultSet = service.query(xpath);
	        // return the number of person element in the persons.xml file
	        i=(int) resultSet.getSize();
	        // increment the size for the new element that we will create 
               i++;
	      	        
			} catch (XMLDBException e) {
				System.out.println("erreur count_persons:"+e.getMessage());
				return 0;
			}
				return i;
	   }
	}
