How to write XML into file using JSP
Hello,
I am parsing a XML file, then updating some of it content and trying to write back the updated file into the same location as an xml document but its not happening correctly....it gets written like this.
&_lt;db_name&_gt;dataext&_lt;/db_name&_gt;
Here is my code......somebody please advise
<%@ page contentType="text/html"%>
<%@ page import="java.io.*,
java.util.*,
org.jdom.*,
org.jdom.input.SAXBuilder,
org.jdom.output.*" %>
<%
String xml_file = "webapps/root/web-inf/admin.xml";
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
Document l_doc = builder.build(new File(xml_file));
Element root = l_doc.getRootElement();
List admin = root.getChildren("db");
Iterator loop = admin.iterator();
while ( loop.hasNext()) {
Element ele = (Element)loop.next();
String state = ele.getChild("state").getText();
String name = ele.getChild("db_name").getText();
String ip = ele.getChild("db_ip").getText();
if(state.equals("Florida")) {
ele.getChild("db_ip").setText("209.11.111.1");
XMLOutputter l_format = new XMLOutputter();
String ls_result = l_format.outputString(l_doc);
root.setText(ls_result);
ls_result = l_format.outputString(l_doc);
%>
<html><head><title></title></head>
<body>
<%
try {
FileOutputStream ostream = new FileOutputStream("c:\\admin.xml");
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeObject(ls_result);
p.flush();
ostream.close();
catch(Exception e) {
out.println(e.toString());
%>
</body>
</html>
thanks amgandhi.....
I found a solution for it, courtesy of this site:
http://www.topxml.com/tutorials/main.asp?id=jdom&page=15
<-********************************************************->
import org.jdom.output.XMLOutputter;
import org.jdom.*;
import java.io.*;
import java.util.*;
// Write a program that creates document with a
// single root element. Add a comment to the
// root element, and assign the document's XML to
// a local string. Finally, write the String to
// System.out, and write the document to a text file.
// For bonus points, allow the user to specify the
// file name on the command line.
public class ws3
public static void main(String[] args)
String filename = "default.xml";
if(args.length > 0) filename = args[0];
Element root = new Element("simple");
Document doc = new Document(root);
Comment cmt = new Comment("A bare document!");
root.addContent(cmt);
XMLOutputter outputter = new XMLOutputter(" ",
true);
String xml = outputter.outputString(doc);
System.out.println(xml);
writeToFile(filename, doc);
private static void writeToFile(String fname,
Document doc)
try {
FileOutputStream out =
new FileOutputStream(fname);
XMLOutputter serializer =
new XMLOutputter(" ", true);
serializer.output(doc, out);
out.flush();
out.close();
catch (IOException e) {
System.err.println(e);
}
Similar Messages
-
How to write to log files using java files from JSP
Anybody knows different options in writing to log files using JSP?
Do you have an example?in the init() method of the servlet put the following
FileOutputStream out = new FileOutputStream("your-log-file");
PrintStream ps = new PrintStream(out);
System.setOut(ps);
System.setErr(ps);load the servlet on startup using <load-on-startup> in web.xml -
How to upload a image file using JSP
hello to all.
i am in the learning stage please help me to upload a image file
using jsp. give the explanation to the code also if possible.
thanks in advance
sincerely
ChezhianYou may find the following articles useful for the JSP/Servlet part:
Uploading files: http://balusc.blogspot.com/2007/11/multipartfilter.html
Downloading files: http://balusc.blogspot.com/2007/07/fileservlet.html -
How to convert XML into XSD Using Altova XML Spy
Hi,
How to convert XML file into XSD Using Altova XML Spy.
I want to use that XSD as an External Def in my IR
Regards
Sumanhi
Following is the path where you could get the PDF's and zip file.
https://www.sdn.sap.com/irj/sdn/howtoguides?rid=/webcontent/uuid/5024a59a-4276-2910-7580-f52eb789194b [original link is broken]
please check out the following Heading, and at the bottom corner you will find the download option where you will get the zip file:
How to Generate XSD Schemas from Existing MDM 5.5 Repositories
You can download xomlite45.jar from sdn
copy the jar file to your java installation location like c:>java in
Java jar xomLite45.jar MyFile.xml
then you get correspondig MyFile.xsd
https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/bf0e8a97-0d01-0010-f0a2-af3b18b7f4eb -
How to put data into textbox using JSP
How can I put data into a textbox using JSP?
This code prints to a html page but I want it inside an text area:
// Print out the type and file name of each row.
while(ftplrs.next())
int type = ftplrs.getType();
if(type == FtpListResult.DIRECTORY)
out.print("DIR\t");
else if(type == FtpListResult.FILE)
out.print("FILE\t");
else if(type == FtpListResult.LINK)
out.print("LINK\t");
else if(type == FtpListResult.OTHERS)
out.print("OTHER\t");
out.print(ftplrs.getName() +"<br>");
}I have tried with the code below:
<textarea name="showDirectoryContent" rows="10" cols="70">
<%
// Print out the type and file name of each row.
while(ftplrs.next())
int type = ftplrs.getType();
if(type == FtpListResult.DIRECTORY)
%>
<%= "DIR\t" %>
<% else if(type == FtpListResult.FILE) %>
<%= "FILE\t" %>
<% else if(type == FtpListResult.LINK) %>
<%= "LINK\t" %>
<% else if(type == FtpListResult.OTHERS) %>
<%= "OTHER\t" %>
<% String temp = ftplrs.getName() +"<br>");
<%= temp > <br>
%>
</textarea>I get the following error:
Location: /myJSPs/jsp/grid-portal-project/processviewfiles_dir.jsp
Internal Servlet Error:
org.apache.jasper.JasperException: Unable to compile Note: sun.tools.javac.Main has been deprecated.
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\grid_0002dportal_0002dproject\processviewfiles_dir_3.java:151: 'else' without 'if'.
else if(type == FtpListResult.FILE)
^
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\grid_0002dportal_0002dproject\processviewfiles_dir_3.java:165: 'else' without 'if'.
else if(type == FtpListResult.LINK)
^
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\grid_0002dportal_0002dproject\processviewfiles_dir_3.java:179: 'else' without 'if'.
else if(type == FtpListResult.OTHERS)
^
C:\tomcat\jakarta-tomcat-3.3.1\work\DEFAULT\myJSPs\jsp\grid_0002dportal_0002dproject\processviewfiles_dir_3.java:193: ';' expected.
String temp = ftplrs.getName() +"");
^
4 errors, 1 warning
at org.apache.tomcat.facade.JasperLiaison.javac(JspInterceptor.java:898)
at org.apache.tomcat.facade.JasperLiaison.processJspFile(JspInterceptor.java:733)
at org.apache.tomcat.facade.JspInterceptor.requestMap(JspInterceptor.java:506)
at org.apache.tomcat.core.ContextManager.processRequest(ContextManager.java:968)
at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:875)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:833)
at org.apache.tomcat.modules.server.Http10Interceptor.processConnection(Http10Interceptor.java:176)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:494)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:516)
at java.lang.Thread.run(Thread.java:536)
Please help???Yes indeed this works:
<textarea name="showDirectoryContent" rows="10" cols="70">
<%
// Print out the type and file name of each row.
while(ftplrs.next())
int type = ftplrs.getType();
if(type == FtpListResult.DIRECTORY)
{%>
<%= "DIR\t" %>
<%} else if(type == FtpListResult.FILE) {%>
<%= "FILE\t" %>
<%} else if(type == FtpListResult.LINK) {%>
<%= "LINK\t" %>
<%} else if(type == FtpListResult.OTHERS) %>
<%= "OTHER\t" %>
<%= ftplrs.getName() %>
<%
%> -
How to upload an html file using jsp and jdbc
Hi,
im trying to upload an html page using JSP and jdbc. but of no success.
my aim is to keep some important html pages in the database.the file size can vary.the file has to be selected from a local machine (through the browser) and uploaded to a remote machine(where the databse resides).
any help/sample code or pointer to any helpful link is appreciated.
thanks in advance
javajar2003When uploading a file, I use a byte array as a temporary buffer..
So, you should then be able to store the byte array in the
database as binary data.
example>
//Temporary Buffer To Store File
byte[] tmpbuffer = new byte[860];
//Some Code To Upload File...
//File Should Now Be In Byte Array
//Get DB Connection and execute Prepared Statement
Connection con=//GET DB CONNECTION;
String sql=insert into TABLE(page) values(?);
PreparedStatement ps=con.prepareStatement(sql);
ps.setBytes(1,tempbuffer);
ps.executeUpdate();
//Close PS and Free DB Connection
..... and this method looks like you dont even have
to store the file in a byte array, you can just give
it the input stream.
ps.setBinaryStream(int, inputStream, int);
You may have to make several attempts at this. I have
uploaded a file and temporarily stored it in a byte array,
but have never from there stored it in the DB as binary
data.. but this looks like it'll work.
Good Luck! -
How to open a doc file using jsp Anchor tag
when i am trying to open a doc file using a jsp it is opening with out proper alignment.
how to open a doc file with proper alignment using Anchor Tag in JSp Page
Hello!
Does some one of you had open a MS word file (.doc) in Java search for a token like [aToken] replace it with another text and then feed it to a stream of save it?
I want to build a servlet to open a well formatted and rich on media (images) ms word document search for tokens and replace them with information form a web form.
Any Ideas?
Thank you in advanced. -
How to download a text file using jsp i
I want to provide download facility in jsp page.
if i have exe download is working . If I have a text / html file it is just displaying I want to make it download How to do it?
regards
subrahmanaymaIf you want to get an "Save as" dialogue, then you need to set the content-disposition in the header to "attachment". The default value is "inline".
Best solution is to create kind of a FileServlet and let it stream the right file as a download to the client. You may find the downloadFile() snippets at the bottom of this article useful: http://balusc.xs4all.nl/srv/dev-jep-pdf.html Put it in a FileServlet and call it using for example . -
How to write an .fds file using Matlab
Hi there,
Is there a way to use Matlab to create a ".fds" (Digital filter design file) so that it can be used in Labview. Alternatively, is there a way to create some arbitrary filter in labview to get this .fds file? More specifically, I am trying to implement some 11th order transfer function and using the "Create a Filter" VI in the labview examples to convert my code to Labview FPGA code. I am using the PXI-7833R (FPGA).
Thanks for the help,
HerrickHello Herrick,
I would recommend using the NI Digital Filter Design Toolkit as it has functions for creating filters, reading filters from files, and for generating LabVIEW FPGA code. Information regarding this tookit can be found at:
http://www.ni.com/toolkits/df_design_toolkit.htm
-Bob
-Bob -
How to upload a graphic file using JSP?
Thanks
The same way you upload any other file. Your form must be declared with multipart/form-data encoding and then, upon the form submit, you will have to parse a multipart request. There are a bunch of libraries around that do that. Our product, TICL (at http://www.kobrix.com) includes a server-side FileUpload component that does it and there a few free, special purpose ones that may help too - do an internet search ;)
-
Convert xml into SAP using dataset
Hi All,
How to convert XML into itab using dataset, in this conversion have any function module available? please give me a sample program (having any). and material also.
Thanks,
Suresh maniarasuHi,
First you need to get the XML file Data into SAP using the Function Module and can populate the data into an inernal table.
TEXT_CONVERT_XML_TO_SAP
DMC_CONVERT_XML_TO_TABLE
or you can use the following classes
CL_RSRD_CONVERTER_XML
CL_WDR_XML_CONVERT_UTIL
CL_EXM_IM_ISHCM_CONV_XML_SAP
Thank U,
Jay.... -
How 2 get the path of a file Using jsp
how 2 get the path of a file Using jsp
i have tried getPath...but i'm geting the error
The method getPath(String) is undefined for the type HttpServletRequest
any idea how 2 get the path of a fileYou need ServletContext#getRealPath().
API documentation: http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String) -
How to upload data into IT0000 using ABAP-HR program
Hello,
I'm required to upload data into multiple infotypes [IT000, 0001, 0002, etc] using single input text file. Can anyone able to guide me, how to upload data into IT0000 using ABAP program ?
Thanks in advance.
Regards
Prabhakar.
Message was edited by:
Prabhakara MuthyalExample code for HR_MAINTAIN_MASTERDATA to COPY IT0002
DATA: INT_0002_FINAL TYPE STANDARD TABLE OF PA0002 WITH HEADER LINE.
DATA: VALUES LIKE PPROP OCCURS 10 WITH HEADER LINE,
RETURN LIKE BAPIRETURN1,.
LOOP AT INT_0002_FINAL.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-PERNR'.
VALUES-FVAL = INT_0002_FINAL-PERNR.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-BEGDA'.
VALUES-FVAL = INT_0002_FINAL-BEGDA.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-ENDDA'.
VALUES-FVAL = INT_0002_FINAL-ENDDA.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-nachn'.
VALUES-FVAL = INT_0002_FINAL-NACHN.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-gblnd'.
VALUES-FVAL = INT_0002_FINAL-GBLND.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-vorna'.
VALUES-FVAL = INT_0002_FINAL-VORNA.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-rufnm'.
VALUES-FVAL = INT_0002_FINAL-RUFNM.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-name2'.
VALUES-FVAL = INT_0002_FINAL-NAME2.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-inits'.
VALUES-FVAL = INT_0002_FINAL-INITS.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-famst'.
VALUES-FVAL = INT_0002_FINAL-FAMST.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-gbdat'.
VALUES-FVAL = INT_0002_FINAL-GBDAT.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-sprsl'.
VALUES-FVAL = INT_0002_FINAL-SPRSL.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-anzkd'.
VALUES-FVAL = INT_0002_FINAL-ANZKD.
APPEND VALUES.
VALUES-INFTY = '0002'.
VALUES-FNAME = 'P0002-natio'.
VALUES-FVAL = INT_0002_FINAL-NATIO.
APPEND VALUES.
* maintain master data
CALL FUNCTION 'HR_MAINTAIN_MASTERDATA'
EXPORTING
PERNR = INT_0002_FINAL-PERNR
ACTIO = 'COP'
BEGDA = INT_0002_FINAL-BEGDA
* ENDDA = INT_0002_FINAL-ENDDA
SUBTY = SPACE
NO_ENQUEUE = SPACE
DIALOG_MODE = '0'
TCLAS = 'A'
IMPORTING
RETURN1 = RETURN
TABLES
PROPOSED_VALUES = VALUES
* MODIFIED_KEYS =
EXCEPTIONS
OTHERS = 1.
IF RETURN IS INITIAL.
WRITE:/' Done....'.
ELSE.
WRITE:/ RETURN-ID, RETURN-TYPE, RETURN-NUMBER, RETURN-MESSAGE_V1, RETURN-MESSAGE_V2, RETURN-MESSAGE_V3, RETURN-MESSAGE_V4.
ENDIF.
CLEAR VALUES.
REFRESH VALUES.
ENDLOOP. -
How to upload an excel file using ABAP.
Hi,
Can anyone please help me in understanding how to upload an excel file using ABAP.
Thanks!!http://diocio.wordpress.com/2007/02/12/sap-upload-excel-document-into-internal-table/
check the link
TYPES: Begin of t_record,
name1 like itab-value,
name2 like itab-value,
age like itab-value,
End of t_record.
DATA: it_record type standard table of t_record initial size 0,
wa_record type t_record.
DATA: gd_currentrow type i.
*Selection Screen Declaration
PARAMETER p_infile like rlgrap-filename.
*START OF SELECTION
call function ALSM_EXCEL_TO_INTERNAL_TABLE
exporting
filename = p_infile
i_begin_col = 1′
i_begin_row = 2′ Do not require headings
i_end_col = 14′
i_end_row = 31′
tables
intern = itab
exceptions
inconsistent_parameters = 1
upload_ole = 2
others = 3.
if sy-subrc <> 0.
message e010(zz) with text-001. Problem uploading Excel Spreadsheet
endif.
Sort table by rows and colums
sort itab by row col.
Get first row retrieved
read table itab index 1.
Set first row retrieved to current row
gd_currentrow = itab-row.
loop at itab.
Reset values for next row
if itab-row ne gd_currentrow.
append wa_record to it_record.
clear wa_record.
gd_currentrow = itab-row.
endif.
case itab-col.
when 0001′. First name
wa_record-name1 = itab-value.
when 0002′. Surname
wa_record-name2 = itab-value.
when 0003′. Age
wa_record-age = itab-value.
endcase.
endloop.
append wa_record to it_record.
*!! Excel data is now contained within the internal table IT_RECORD
Display report data for illustration purposes
loop at it_record into wa_record.
write:/ sy-vline,
(10) wa_record-name1, sy-vline,
(10) wa_record-name2, sy-vline,
(10) wa_record-age, sy-vline.
endloop. -
How to read a text file using Java
Guys,
Good day!
Please help me how to read a text file using Java and create/convert that text file into XML.
Thanks and God Bless.
Regards,
I-Talkpublic void fileRead(){
File aFile =new File("myFile.txt");
BufferedReader input = null;
try {
input = new BufferedReader( new FileReader(aFile) );
String line = null;
while (( line = input.readLine()) != null){
catch (FileNotFoundException ex) {
ex.printStackTrace();
catch (IOException ex){
ex.printStackTrace();
}This code is to read a text file. But there is no such thing that will convert your text file to xml file. You have to have a defined XML format. Then you can read your data from text files and insert them inside your xml text. Or you may like to read xml tags from text files and insert your own data. The file format of .txt and .xml is far too different.
cheers
Mohammed Jubaer Arif.
Maybe you are looking for
-
Nls support with oracle xml parser in java
I'm using The JAXP interface to manipulate xml with xsl. the implementation that I use is Oracle xml parser. My xml contains a lot of hebrew chars, and in the result stream each char translated into 5-6 chars, which makes the result to be 5-6 times b
-
Hello experts, I would like to know if it's possible to get the sessionID of the current session of an user within an ABAP report - similar to getting other system-parameters, such as system time, username etc. I couldn't find a system-parameter-fiel
-
How do I transfer library on i pod to laptop
52 year old computer idiot here. My main computer crashed and I lost everything. I now have a hp laptop with windows 7. I downloaded i tunes to the laptop but when I connect my i pod it says I am already synced to another library. I have been searchi
-
i want to know the advantages of using LISP over BGP? could someone explain it to me here. thanks Kashif
-
Synching all calendar events (more than 8 weeks in the future)
How can I do this? I am trying to synchronize to a SonyEricsson K750i phone and I would like all my calender on it (which has entries more than a year in advance). Why is there an 8 week limitation? Can I override this somehow? Thanks, -- Kris