|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am in the process of converting ASP code to JSP, Java.
There are so many Java classes to write to a file. At the present time I want to write XML to a file but which Java class should I use and how do I proceed? The following is ASP code. <% QUOT = Chr(34) CRLF = VbCrlf Response.ContentType = "text/xml" 'create new file, overwriting any existing one Set objFile = objFSO.CreateTextFile(strFileName, True) 'write XML page headings to file strLine = "<?xml version=" & QUOT & "1.0" & QUOT & " ?>" objFile.WriteLine strLine & CRLF strLine ="<AccessRequest xml:lang=" & QUOT & "en-US" & QUOT & ">" objFile.WriteLine strLine & CRLF strLine = " <AccessLicenseNumber>" & logLicense & "</AccessLicenseNumber>" objFile.WriteLine strLine & CRLF strLine = " <UserId>" & logUserId & "</UserId>" objFile.WriteLine strLine & CRLF strLine = " <Password>" & logUserPassword & "</Password>" objFile.WriteLine strLine & CRLF strLine = "</AccessRequest>" objFile.WriteLine strLine & CRLF %> ******************************************** The following JSP code creates the file. What should I do next? <%! void WriteXMLTransHeader1() { String strPathInfo = request.getServletPath(); String strRealPath = getServletConfig().getServletContext ().getRealPath(strPathInfo); int intSlashIndex = strRealPath.lastIndexOf("\\"); String strNewRealPath = strRealPath.substring(0, intSlashIndex + 1); String strDirectory = strNewRealPath + "UPSTrans"; String strFileName = "Order" + logOrderId + "A" + ".xml"; File f1 = new File(strDirectory, strFileName); f1.createNewFile(); System.out.println("File: Order" + strFileName + "created"); %> ********************** Thanks for your help, Robin |
|
#2
|
|||
|
|||
|
As you may or may not know, there are tons of Streams you can use for i/o in java. Sometimes that can seem daunting. What you want to use is a FileOutputStream or a FileWriter. There are minor differences between them and either should work. There is a BufferedOutputStream that buffers output so that writing to the hard drive is done in a more efficient manner. There is also a PrintStream for easy formatting of output.
The nice thing about Streams is that they can be wrapped. So, you could make a FileOutputStream and wrap it in a BufferedOutputStream, and then wrap THAT in a PrintStream. This gives you the benefits of all. Here is an example I found on Suns site: Code:
PrintStream pStr = null;
System.out.println("Entering try statement");
int i;
pStr = new PrintStream(
new BufferedOutputStream(
new FileOutputStream("OutFile.txt")));
for (i = 0; i < size; i++)
pStr.println("Value at: " + i + " = " + victor.elementAt(i));
pStr.close();
This example has the files name in a string for the FileOutputStreams constructor, but you can give it File object also. You should be able to use this with minor modifications. Stream tutorial Last edited by Nemi : April 17th, 2003 at 04:48 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > JSP - Writing to XML File |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|