Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 17th, 2003, 04:12 PM
RobinR RobinR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: New Jersey
Posts: 77 RobinR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 2 m 21 sec
Reputation Power: 6
Lightbulb JSP - Writing to XML File

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

Reply With Quote
  #2  
Old April 17th, 2003, 04:45 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > JSP - Writing to XML File


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway