This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes save a xml file using java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "save a xml file using java" Watch "save a xml file using java" New topic
Author

save a xml file using java

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I am getting a steam of xml data to a java Servlet.
I need to write a code in java servlet. That
captures the content of xml file as it is
and saves the file with a unique name(using one
of the fields in the xml file). Does any body
has any code that does this., can anybody give
me some direction.
I will appreciate someone's help


Bhasker Reddy
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
I am moving this to the Servlets forum, since this is not in the scope of SCJP.
regds.
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Frank Daly
Ranch Hand

Joined: Mar 31, 2000
Posts: 139
Reddy,
I generate an XML file from a String. This is slightly different from your situation but I hope it'll give you some ideas.
My code is
String xmlString = ("hope this works");
try
{
String thisFile = new String(req.getParameter("fileName") + ".xml");
OutputStreamWriter oos = new OutputStreamWriter (new FileOutputStream(thisFile));
oos.write (xmlString);
oos.close();
oos = null;
thisFile=null;
}
catch (IOException ioe)
{
System.out.println("IO error: " + ioe);
}
This should generate a file with the name of the fileName request parameter with the xml extension. The text in the file will be the contents of the String xmlString.
Frank
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
hOW DO I HAVE SAVE THIS FILE ON A PARTICULAR
DIRECTORY OF A SERVER.
WHAT IF MY STRING IS EXACTLY AN XML FILE LIKE
<name>bill</name>
<address>3118 richardson ave</address>
<phone_num>
<office>98723242</office>
<residence>24242324</residence>
<cell>2424234</cell>
</phone_num>
something like that is coming as a stream
i need to capture it exactly as it, write to a
file and also save the file
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
There is nothing magical about servlets with respect to files, you can use any normal Java file operations. You can set a directory path with a servlet initialization parameter, use that path to create the file and then write to it.
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
Do you have any example of the code. I am new to
Java doesn't have much programming experience.
Can you just give some sample code, servlet initialization directory path and writing the
file to a particular file name
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I am using the following java file to capture
the contents of the xml file
Java Program
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/** This servlet is a template for the CBS servlet. In the
* "doPost" method, it reads an XML file as a string from the HttpRequest's
* input stream, and writes a response to the HttpResponse's
* output stream.
*/
public class CbsTestServlet extends HttpServlet {
/*testing xml storage functionality*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {

System.out.println("Start CBSTestServlet doPost");

// Expect plain text - the XML comes across as text.
response.setContentType("text/plain");
// Open input and output streams.
ServletInputStream inputStream = request.getInputStream();
ServletOutputStream outputStream = response.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outputStream));

String line = "";
String xmlString = "";
while ( (line = in.readLine()) != null ) {
System.out.println(xmlString );
xmlString += line + "\n";
}

//write to a output stream writer
try {
System.out.println("I am trying to save the file");
String thisFile = new String("xmlfile2" + ".xml");
OutputStreamWriter oos = new OutputStreamWriter(new FileOutputStream(thisFile));
oos.write(xmlString);
oos.close();
oos=null;
thisFile=null;

}
catch(IOException ioe)
{
System.out.println("i did something wrong");
System.out.println("IO error: " + ioe);
}
out.flush();
out.close();
in.close();
System.out.println("End CBSTestServlet doPost");
return;
}
}
***************************************
Input XML FILE
<Name>Bhasker</Name>
<Address>3118</address>
<phone>21332</phone>
<city>phoenix</city>
***************************************
OUTPUT THAT I GOT INSIDE THE XML2FILE.XML
xmlstring=%3CName%3EBhasker%3C%2FName%3E%0D%0A%3CAddress%3E3118%3C%2Faddress%3E%0D%0A%3Cphone%3E21332%3C%2Fphone%3E%0D%0A%3Ccity%3Ephoenix%3C%2Fcity%3E
i DON'T KNOW HOW TO GET RID OF ALL THIS JUNK
AND GET THE XML FILE AS IT IS.
i AM GETTING THIS XML FILE AS A HTTP STREAM OR
SUBMITTED BY ANOTHER WEB PAGE.
DOES ANYONE HAVE ANY BETTER IDEAS
Frank Daly
Ranch Hand

Joined: Mar 31, 2000
Posts: 139
Hi Bhasker,
I had the same problem with the amperstand, &, character. There are 4 characters that you must escape to ensure your file will have meaningful content. The table of characters and their escapes are below
< <
& &
" "e;
' '
You must include the semi-colon character. You must replace the characters above with their escapes.
Hope this helps
Frank
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
How do i replace those characters when it is part of
stream
can you advise me how to do it
[ January 15, 2002: Message edited by: Bhasker Reddy ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
This really seems pretty odd to me - the Java output routine to write a String to the OutputStreamWriter surely can't be doing the substitution of the escaped forms for < etc.
They must be getting converted somewhere else.
What is that System.out.println( xmlString ) line showing?
Bill
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
system.out.println(xmlString)
is printing the same thing
xmlstring=%3CName%3EBhasker%3C%2FName%3E%0D%0A%3CAddress%3E3118%3C%2Faddress%3E%0D%0A%3Cphone%3E21332%3C%2Fphone%3E%0D%0A%3Ccity%3Ephoenix%3C%2Fcity%3E
I am submiting the following xml file through a web page.
<Name>Bhasker</Name>
<Address>3118</address>
<phone>21332</phone>
<city>phoenix</city>
Is there any function in JAva that i can use
to replace
%3C with <
%/*%3C with <
%3E with >
%2F with /
%0D%0A with \n
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I AM USING THIS CODE AND GETTING THIS MESSAGE
***************
INPUT
**************
<Name>Bhasker</Name>
<Address>fernhurst</Address>
<city>Richardson</city>
**********************
PROGRAM I AM USING
**********************-
/*testing xml storage functionality*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {

System.out.println("Start CBSTestServlet doPost");

// Expect plain text - the XML comes across as text.
response.setContentType("text/plain");
ServletInputStream inputStream = request.getInputStream();
ServletOutputStream outputStream = response.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
//BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outputStream));
String thisFile = new String("xmlfile6" + ".xml");
OutputStreamWriter oos = new OutputStreamWriter(new FileOutputStream(thisFile));

String line = "";
while ( (line = in.readLine()) != null ) {
System.out.println("line" + line);
line = line.substring(13);
//line = line.replace('%', ' ');
line = line.trim();
line = line.replace('+', '\n');


oos.write(line);
}
thisFile = null;
oos.flush();
oos.close();
//out.flush();
//out.close();
System.out.println("End CBSTestServlet doPost");
return;
}
********************************
OUTPUT
**********
Name%3EBhasker%3C%2FName%3E
%0D%0A%3CAddress%3Efernhurst%3C%2FAddress%3E
%0D%0A%3Ccity%3ERichardson%3C%2Fcity%3E
DO YOU GUYS KNOW HOW CAN I GET RID OF THIS JUNK
I APPRECIATE IF ANY BODY CAN HELP ME
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
OK - the browser is doing a urlencode operation on the text before sending it.
You need to use this class in the java.net package
public class URLDecoder
extends Object
The class contains a utility method for converting from a MIME format called "x-www-form-urlencoded" to a String
To convert to a String, each character is examined in turn:
The ASCII characters 'a' through 'z', 'A' through 'Z', and '0' through '9' remain the same.
The plus sign '+'is converted into a space character ' '.
The remaining characters are represented by 3-character strings which begin with the percent sign, "%xy", where xy is the two-digit hexadecimal representation of the lower 8-bits of the character.
-- so for each line that is read, do the following before saving it.
line = URLDecoder.decode( line );
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
Unfortunately our team uses visual age 3.0. It
contains Java 1.1.7.
URLDecoder class is not present in it's java class
libraries.
It has URLEncoder,URLStreamHandler, URLStreamHandlerFactory classes in it but not
URLDecoder.
I am not sure if i can download a specific package from java.sun.com like java.net.* and incorporate it in my Visual age.
Again guys thanks for all your help
Meng Tan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
Hi,
May I ask whether your file upload is done using a POST request which has a content type of "application/x-www-form-urlencoded".
Is the XML file content a parameter value in the POST request?
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: save a xml file using java
 
Similar Threads
Saving xml file through a java Servlet to a server
Map object through servlet?
Cannot Invoke Servlets
Which JSTL jar files for tomcat 5.5?
how to read and write, save a xml stream passed to a Servlet