Solved

Creating a XML file in java

Posted on 2007-11-26
3
160 Views
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;
import java.util.zip.*;
import java.io.*; 
 
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!"); 
	doit.addActionListener(this); 
 
	//javax.swing.border.TitledBorder dragBorder = new javax.swing.border.TitledBorder( "Drop 'em" );
        final javax.swing.JTextArea text = new javax.swing.JTextArea();
	frame.getContentPane().add( 
				   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( java.io.File[] files )
            {   for( int i = 0; i < files.length; i++ )
                {   try
                    {   
			text.append( files[i].getCanonicalPath() + "\n" );
			remember.add(files[i].getCanonicalPath()); 
		    }   // end try
                    catch( java.io.IOException e ) {}
                }   // end for: through each dropped file
            }   // end filesDropped
        }); // end FileDrop.Listener
 
        frame.setBounds( 100, 100, 300, 400 );
        frame.setDefaultCloseOperation( frame.EXIT_ON_CLOSE );
        frame.show();
    }
 
    
    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 = "target.zip";
	    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 = in.read(buf)) > 0) {
		    out.write(buf, 0, len);
		}
		
		// Complete the entry
		out.closeEntry();
		in.close();
	    }
	    
	    // Complete the ZIP file
	    out.close();
	} catch (IOException e) {
	}
	System.out.println("You have been ZIPPED"); 
    }
}

Open in new window

0
Comment
Question by:Jaziar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

Expert Comment

by:mrcoffee365
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.
<filename>file1</filename>
0
 

Author Comment

by:Jaziar
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

Example:

C://Admin/Desktop/File.txt
C://tmp/file2.txt

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

Example:
File.txt
file2.txt

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

Accepted Solution

by:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 262
Maven Project: Hibernate Dependencies Conflict 10 85
swing controls 2 35
wild fly 8 startup error 2 75
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 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