Creating a XML file in java

Posted on 2007-11-26
Last Modified: 2010-03-30
I have a program that allows the user to drag and drop files in to a window.  I then click a button and the program zips the files that have been dragged.  Is it possible that when the zip file button is clicked the program can create a xml file.  The file would look something like this

Jaziar    <------Username
Nov, 25, 2007    <-----Date
file 1
file 2                 ,<----- then the names of all the files in the window

I will attach the code that zips
import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.util.Vector;



class UploadDriver implements ActionListener {

    final Vector<String> remember = new Vector<String>(); 


    public UploadDriver () {

        javax.swing.JFrame frame = new javax.swing.JFrame( "FileDrop" );

	javax.swing.JButton doit = new javax.swing.JButton("Upload Files!"); 


	//javax.swing.border.TitledBorder dragBorder = new javax.swing.border.TitledBorder( "Drop 'em" );

        final javax.swing.JTextArea text = new javax.swing.JTextArea();


				   new javax.swing.JScrollPane( text ), 

				   java.awt.BorderLayout.CENTER );

        frame.getContentPane().add(doit, java.awt.BorderLayout.SOUTH); 


        new FileDrop( System.out, text, /*dragBorder,*/ new FileDrop.Listener()

        {   public void filesDropped([] files )

            {   for( int i = 0; i < files.length; i++ )

                {   try


			text.append( files[i].getCanonicalPath() + "\n" );


		    }   // end try

                    catch( e ) {}

                }   // end for: through each dropped file

            }   // end filesDropped

        }); // end FileDrop.Listener

        frame.setBounds( 100, 100, 300, 400 );

        frame.setDefaultCloseOperation( frame.EXIT_ON_CLOSE );;



    public void actionPerformed(ActionEvent ae) {

	System.out.println("Time to zip it, zip it good"); 

	System.out.println("Looking to zip " + remember.size() + " files"); 

	byte[] buf = new byte[1024];


	try {

	    // Create the ZIP file

	    String target = "";

	    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));


	    // Compress the files

	    for (int i=0; i<remember.size(); i++) {

		FileInputStream in = new FileInputStream(remember.elementAt(i));


		// Add ZIP entry to output stream.

		out.putNextEntry(new ZipEntry(remember.elementAt(i)));


		// Transfer bytes from the file to the ZIP file

		int len;

		while ((len = > 0) {

		    out.write(buf, 0, len);



		// Complete the entry





	    // Complete the ZIP file


	} catch (IOException e) {


	System.out.println("You have been ZIPPED"); 



Open in new window

Question by:Jaziar
  • 2
LVL 27

Expert Comment

ID: 20353255
Sure.  Make a new .xml file outside the loop where you manipulate the list of files.  Write the 2 header lines to the top, then for each file, write the file name to the .xml file.

If you plan to use XML syntax, then surround the data with XML tags, e.g.

Author Comment

ID: 20353331
I know this is some what of a second question, but I will create a new question if you experts think I should.

But if you look at the above code, where it is zipping the files it is putting in complete paths to the files



So when it unzips the full path is still part of the file.  Is it possible to only zip the file? and not the complete path


That way when I move the zip, it is not dependent on the dir structure
LVL 27

Accepted Solution

mrcoffee365 earned 500 total points
ID: 20353903
Use the file's name -- the last element in the absolute path -- as the new ZipEntry.

And yes, it's a very different question.  But if we don't have to spend much time on it, then we'll just include it here.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unable to open debugger port in Intellij idea 6 143
groovy example issue 10 90
bitbucket vs gitbucket 3 57
ArrayIndexOutOfBoundException 9 67
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now