Create xml file and write data in to it:
Hi All,
I have a small requirement here.....
I have a java program which reads a xml file.
Now i have stored the contents of that file in to a java object.
I want a sample code now which would take theabove mentioned java object as an input ,create a xml file and write these entries in to the file.
Can anyone help me in this?
Your help would be appreciated.
Thanks in advance.
vrkr
Hmmm. There is nothing special about an XML file to any other file. The data is simply formatted in a certain way. So to write an XML file, it is no different to writing any other form of file in Java (assuming you know the structure of the xml format to write - which you should)
If you don't know how to write files using Java I would suggest using the Java Tutorial found on java.sun.com and do some reading up about I/O since this is fairly fundamental.
http://java.sun.com/docs/books/tutorial/
http://java.sun.com/docs/books/tutorial/essential/io/index.html
Similar Messages
-
Please recommend if we have options to read xml file and insert data into table without a temporary table.
DECLARE @data XML;
SET @data =N'<Root>
<List RecordID="946236" />
<List RecordID="946237" />
<List RecordID="946238" />
<List RecordID="946239" />
<List RecordID="946240" />
</Root>'
INSERT INTO t (id) SELECT T.customer.value('@RecordID', 'INT') AS id
FROM @data.nodes('Root/List')
AS T(customer);
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
How can FMS create a text file and write data into it in the Server application folders?
Recently, I writed a programe about creating a text file and writing data into it in the server application folder. My code is as following:
var fileObj = new File("/MyApp/test.txt");
if( fileObj != null)
if(fileObj.open( "text", "append"))
fileObj.write( " ———— Chat Info Backup ————\r\n" );
fileObj.close( );
trace("Chat info backup document :" + fileObj.name + " has been created successfully!");
But when I run it, FMS throw the error as following: File operation open failed ; TypeError: fileObj has no properties.
Can you help me ? Thanks in advance.
Supplement: The text file named test.txt doesn't exist before create the fileObj, an instance of File Class.Is MyApp the name of the application directory, or is it a child of the application directory? If myApp is the app name, just use test.txt as the path flag in the file constructor.
-
Reading an XML file and write the contents to another xml file in java
Hi,
I am new to xml parsing.My requirement is that I am getting a message (xml) using ibm MQ in the ByteArrayInputStream format.I have to read this xml message and write to another file.
I am creating a POC for this.
First I used simple reading and writing concept but the output is "java.io.FileInputStream@3e25a5 "
Sample xml file
- <Client>
<ClientId>1234</ClientId>
<ClientName>STechnology</ClientName>
<DTU_ID>567</DTU_ID>
<ClientStatus>ACTIVE</ClientStatus>
- <LEAccount>
<ClientLE>678989</ClientLE>
<LEId>56743</LEId>
- <Account>
<AccountNumber>9876543678</AccountNumber>
</Account>
</LEAccount>
- <Service>
<Cindicator>Y2Y</Cindicator>
<PrefCode>980</PrefCode>
<BSCode>876</BSCode>
<MandatoryContent>MSP</MandatoryContent>
</Service>
</Client>
code:
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ByteArrayInputStreamToXml {
public static void main(String srg[]) throws IOException{
InputStream inputStream= new FileInputStream("C:\\soft\\test2\\sample1.xml");
byte currentXMLBytes[] = inputStream.toString().getBytes();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(currentXMLBytes);
OutputStream out = new FileOutputStream("C:\\soft\\test\\data.xml");
int read=0;
byte[] bytes = new byte[1024];
while((read = byteArrayInputStream.read(bytes))!= -1){
out.write(bytes, 0, read);
out.write( '\n' );
inputStream.close();
out.flush();
out.close();
System.out.println("New file created!");
Please suggest me how can I use DOM/SAX parser ,I can see several code on net for reading xml file using SAX/DOM parser but writing an xml file after reading it using ByteArrayInputStream I am not getting .A help through some example Link will also be helpful for me.
Thanks
Sumit
Edited by: user8687839 on Apr 30, 2012 2:37 AM
Edited by: user8687839 on Apr 30, 2012 2:43 AMThanks I got the result.
package com.sumit.collections;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ByteArrayInputStreamToXml {
public static void main(String srg[]) throws IOException{
InputStream inputStream= new FileInputStream("C:\\soft\\test2\\sample1.xml");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead; byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead); } buffer.flush();
byte currentXMLBytes[]= buffer.toByteArray();
/* byte currentXMLBytes[] = inputStream.toString().getBytes();*/
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(currentXMLBytes);
OutputStream out = new FileOutputStream("C:\\soft\\test\\data.xml");
int read=0;
byte[] bytes = new byte[1024];
while((read = byteArrayInputStream.read(bytes))!= -1){
out.write(bytes, 0, read);
out.write( '\n' );
inputStream.close();
out.flush();
out.close();
System.out.println("New file created!");
} -
Parse XML file and extract data
I'd like to parse an XML file and get some data extracted as columns.
Input file country.xml:
<?xml version="1.0" encoding="UTF-8"?>
<MAS Action="Insert">
<Country ObjectId="100000000000000009" VersionId="8"><Id>1</Id><NlTexts><Name Language="de">Land1</Name><Name Language="en">Country1</Name></NlTexts></Country>
<Country ObjectId="100000000000000033" VersionId="2"><Id>2</Id><NlTexts><Name Language="de">Land2</Name><Name Language="en">Country1</Name></NlTexts></Country>
</MAS>
I'd like to parse the xmlfile in order to get the following output
Required result:
col1 col2 col3
1 Land1 Country1
2 Land2 Country2
or alternatively
col1 col2
1 Land1
1 Country1
2 Land2
2 Country2
I tried extract-function
select extract((XMLTYPE(BFILENAME('XML_DAT_DIR', 'country.xml'),
NLS_CHARSET_ID('AL32UTF8'))) , '/*/*/Id') as "xdata"
from dual;
xdata
<Id>1</Id><Id>2</Id>
and XMLTABLE (but how can I add the countries now)
SELECT *
FROM XMLTABLE('/*/*/Id'
PASSING XMLTYPE(BFILENAME('XML_DAT_DIR', 'country.xml'),
NLS_CHARSET_ID('AL32UTF8'))
COLUMN_VALUE
<Id>1</Id>
<Id>2</Id>
DB version 11.2.0.3 on Windows 64bit
Thanks,
TimHere are a few examples.
For your required output :
SELECT *
FROM XMLTable(
'/MAS/Country'
passing XMLType(bfilename('TEST_DIR', 'country.xml'), nls_charset_id('AL32UTF8'))
columns col1 number path 'Id'
, col2 varchar2(30) path 'NlTexts/Name[1]'
, col3 varchar2(30) path 'NlTexts/Name[2]'
or, if the Language attribute is significant :
SELECT *
FROM XMLTable(
'/MAS/Country'
passing XMLType(bfilename('TEST_DIR', 'country.xml'), nls_charset_id('AL32UTF8'))
columns col1 number path 'Id'
, col2 varchar2(30) path 'NlTexts/Name[@Language="de"]'
, col3 varchar2(30) path 'NlTexts/Name[@Language="en"]'
For your alternate output :
SELECT x1.col1
, x2.col2
--, x2.col3
FROM XMLTable(
'/MAS/Country'
passing XMLType(bfilename('TEST_DIR', 'country.xml'), nls_charset_id('AL32UTF8'))
columns col1 number path 'Id'
, names xmltype path 'NlTexts/Name'
) x1
, XMLTable(
'/Name'
passing x1.names
columns col2 varchar2(30) path '.'
--, col3 for ordinality
) x2
(uncomment col3 to see what it does)
or, in a shorter way :
SELECT *
FROM XMLTable(
'for $i in /MAS/Country
, $j in $i/NlTexts/Name
return element r { $i/Id, $j }'
passing XMLType(bfilename('TEST_DIR', 'country.xml'), nls_charset_id('AL32UTF8'))
columns col1 number path 'Id'
, col2 varchar2(30) path 'Name' -
How to read XML file and write into another XML file
Hi all, I am new to JAVAXML.
My problem is I have to read one XML file and take some Nodes from that and write these nodes into another XML file...
I solved, how to read XML file
But I don't know how to Write nodes into another XML.
Can anyone help in this???
Thanks in advance..This was answered a bit ago. There was a thread called "XML Mergine" that started on Sept 14th. It has a lot of information about what it takes to copy nodes from one XML Document object into another.
Dave Patterson -
How to activate another worksheet in excel file and write data into it
Hi,
I am writing an automation program to collect test data and write the data to an excel file.
The excel file has several worksheets and now I can only write data to one sheet. Can anyone please let me know how to activate another worksheet and write data into it? Thank you very much.You can do a search in the Example Finder for more Excel VIs.
They will give you a clearer idea of how to go about doing things in the way you need.
- Partha
LabVIEW - Wires that catch bugs! -
Not being able to create TDMS file and write to it
I m new to labview and lately i have been trying to code a RT vi which allows me create a TDMS file and write into it. I have seen a couple of examples on web. and this is what i implemented (refer the snippet i attached) after going through them.
Now the problem is, there is no file being created on my desktop. I dont understand what is wrong. I may be missing some simple part. Please help me to figure it out
Solved!
Go to Solution.
Attachments:
RT_write to TDMS.PNG 10 KBHi Rex,
usually "RT targets" are NOT capable of accessing your Windows PC desktop as they are computers of their own…
Write to the RT storage and copy the files created there using FTP to your PC!
Best regards,
GerdW
CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
Kudos are welcome -
Create XML file from table data
Dear All,
with dataservice 4.0, I want to create an XML file from a table data.
Table have a single column but more record, for example:
0001000488;100;EUR;
0001000489;200;EUR;
0001000450;300;EUR;
My desired XML output:
<Data>
0001000488;100;GBP;
0001000489;200;EUR;
0001000450;300;EUR;
</Data>
I try with a sample query but the sistem write only the last record in XML file:
<Data>
0001000450;300;EUR;
</Data>
Can everyone help me?
Thank in advance.
SimoneHello
That is a very simple (also odd) XML document structure, and as such doesn't require use of the XML target. It can be easily acheived by writing a normal file with a header and footer, which is acheived using a row_generation and a query to generate the hard coded open and close tags.
Michael -
Comparing an excel file to and xml file and appending data to the xml file
I have an xml file (mapsource) and an excel file. One of the columns in the excel file matches up with a node in the xml file. I want to be able to loop through both files and add a new node to the xml file where the one of the columns in the excel file matches a node in the xml file.
I can create a query object out of the excel dosument using the cfspreadsheet tag.
I'm able to use xmlparse to create a xml document object from the xml file.
What I would like to do is add col_3 from the excel spreadsheet as a new node when col_2 is equal to gpx.wpt.name.xmltext of the xml object.
Any help or direction would be very helpful!Hi,
Thank you for your reply. But I have to attach an excel file from a particular path (C:\TEMP\TEST.XLS) and I have to send that excel file to the user inbox.That excel file has multiple sheets with data. Can you please provide me any code is available.
Thanks and Regards
venkat. -
Read multiple files and write data on a single file in java
Hello,
I am facing difficulty that I want to read multiple files in java and write their data to a single file. Means Write data in one file from multiple files in java
Please help me out.
Naveed.algorithm should be something like:
File uniqueFile = new File();
for (File f : manyFilesToRead)
while (readingF)
write(dataFromF, intoUniqueFile); -
Creating xml file and inert tin to xml file in clob column
i have a table
CREATE TABLE XMLCLOB of XMLType
XMLTYPE store AS CLOB;
now i want to create a xml file by qurying emp table ie select sal,empname from emp.
the cretaed xml file is saved in the xmlclob table how to do that?You will need to use Oracle XML function EXTRACT or EXTRACTVALUE in order to read the data before inserting into Oracle table. These functions need to be used in select statement.
Syntax:
EXTRACT(XMLType_Instance>, <XPath_string>, <namespace_string>)
EXTRACTVALUE(XMLType_Instance>, <XPath_string>, <namespace_string>) -
Creating XML file and transport to GIS (gentran integration suite)
Hello Experts,
I have a requirement. I need to convert the data from an internal table to XML Format file , and send that XML file to
GIS application [ gentran integration suite - middleware applicationa ]
i need to save that file to Unix-Filesystem and call an FTP transfer routine to GIS.
[ Purpose of GIS --> Transforming data (using services) from different applications so that it can be communicated to other
systems.Moving and communicating data between different systems external to Gentran Integration Suite,using adapters. ]
Please kindly reply me....
ThanksI do not think that you need to know more than the basics of Gentran, as you will have to upgrade PI, not Gentran.
Just check the Homepage of Gentran to find an overview about it:
http://www.sterlingcommerce.com/products/b2b-integration/sterling-integrator/
or find more information with google. -
Creating a File and writing data to it
hi,
I have a problem when creating a file,my OS is LINUX,the file not getting creating,after creating the file,i wanna write some data into it,please help me... :-(if you are using JSR 75 API then the file is creating in wtkdir/appd/defaultcolorphone/filesystem/.............
-
Creating XML files from one data file in a java program
<statement>
<name>John Dow </name>
<address>123 Main</address>
<phone>972-213-3434</phone>
</statement>
<statement>
<name>Jane Dow </name>
<address>345 Main</address>
<phone>972-213-3534</phone>
</statement>
<statement>
<name>Marry Dow </name>
<address>678 Main</address>
<phone>972-213-3435</phone>
</statement>
Thanks,
BillSo how does it take this file
file>
<statement>
<name>John Dow </name>
<address>123 Main</address>
<phone>972-213-3434</phone>
</statement>
<statement>
<name>Jane Dow </name>
<address>345 Main</address>
<phone>972-213-3534</phone>
</statement>
<statement>
<name>Marry Dow </name>
<address>678 Main</address>
<phone>972-213-3435</phone>
</statement>
</file>
and create these three different one?
1.)
<statement>
<name>John Dow </name>
<address>123 Main</address>
<phone>972-213-3434</phone>
</statement>
2.)
<statement>
<name>Jane Dow </name>
<address>345 Main</address>
<phone>972-213-3534</phone>
</statement>
3.)
<statement>
<name>Marry Dow </name>
<address>678 Main</address>
<phone>972-213-3435</phone>
</statement>
Is it not going to just grab each element after the file tag? I'm just confused about that.
Maybe you are looking for
-
Printer is no longer recognized.
After years of effortless wireless printing, I can no longer print. The System Preference settings are fine, specifying the printer as HP psc 1300 series. Mountain Lion tells me only that "PRINT SERVICE IS NOT AVAILABLE!" or "The printer is not conn
-
Sender File Adapter not able to get file via FTP from an external system
Hi, I have a Sender File Adapter using FTP which polls a server every 10 seconds looking for a file ".xml". The adapter will connect and pick up the waiting file, but sometimes the adapter not able to get file via FTP from an external system. The fi
-
Problem with junk folder showing up when I don't want it and have set to not have it
Just updated Thunderbird. Have settings set to NOT create junk folder, but a junk folder keeps showing up with emails in it (that are not junk!). Also, and even worse, there is no notification that there are new emails in the junk folder- I have to c
-
Can someone please tell me what I need to do to use my AppleCare to replace my charger? And if I don't have my AppleCare info but have the email from when I activated it, is that all I need?
-
Hi , I'm stuck on an Abap program I have to execute a Bex Query then store the retreived data, the old logic uses classe cl_RSR_Query_variables, this doesn't exist any more. I want to keep my logic and substitute only the type of my query variable he