Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Swing-AWT How to create XML from Swings

Have Programming Question? Ask it here!
 
 
 


Abhilash Boinapally
How to create XML from Swings
2 Answer(s)      2 years and a month ago
Posted in : Swing AWT


How to create XML using Swings. I have a Swing GUI and capturing all data from it.When i click on submit, an xml should be created with proper binding for all the elements. < Student > < Name >abc < /Name > < id >123< /id > < /student >


Advertisement
View Answers

July 11, 2012 at 3:39 PM


Here is a code that accepts the data from the user through swing components and display it in xml file.

import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class CreateXML extends JFrame{
JButton ADD;
JPanel panel;
JLabel label1,label2,label3,label4;
final JTextField text1,text2,text3,text4;

public static void createXmlFile(Document doc,String name,String address,String contactNo,String email) throws Exception {
Element root = doc.createElement("Student");
doc.appendChild(root);
Element element1 = doc.createElement("Name");
root.appendChild(element1);
Text text1 = doc.createTextNode(name);
element1.appendChild(text1);

Element element2 = doc.createElement("Address");
root.appendChild(element2);
Text text2 = doc.createTextNode(address);
element2.appendChild(text2);

Element element3 = doc.createElement("ContactNo");
root.appendChild(element3);
Text text3 = doc.createTextNode(contactNo);
element3.appendChild(text3);

Element element4 = doc.createElement("Email");
root.appendChild(element4);
Text text4 = doc.createTextNode(email);
element4.appendChild(text4);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();

File file = new File("c:/student.xml");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));
bw.write(xmlString);
bw.flush();
bw.close();
}
CreateXML(){
setLayout(null);
label1 = new JLabel();
label1.setText("Name:");
text1 = new JTextField(20);

label2 = new JLabel();
label2.setText("Address:");
text2 = new JTextField(20);

label3 = new JLabel();
label3.setText("Contact No:");
text3 = new JTextField(20);

label4 = new JLabel();
label4.setText("Email:");
text4 = new JTextField(20);

ADD=new JButton("ADD");

label1.setBounds(20,50,100,20);
text1.setBounds(140,50,100,20);

label2.setBounds(20,80,100,20);
text2.setBounds(140,80,100,20);

label3.setBounds(20,110,100,20);
text3.setBounds(140,110,100,20);

label4.setBounds(20,140,100,20);
text4.setBounds(140,140,100,20);
ADD.setBounds(140,200,100,20);

add(label1);
add(text1);
add(label2);
add(text2);
add(label3);
add(text3);
add(label4);
add(text4);
add(ADD);

ADD.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){

String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=text4.getText();
try{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
createXmlFile(doc,value1,value2,value3,value4);
System.out.println("Xml File Created Successfully");
}
catch(Exception E){}
}
});
}
public static void main(String arg[]){
CreateXML frame=new CreateXML();
frame.setSize(300,300);
frame.setVisible(true);
}
}


July 11, 2012 at 4:33 PM


Thanks Deepak..very informative.



Related Tutorials/Questions & Answers:
How to create XML from Swings
How to create XML from Swings  How to create XML using Swings. I have a Swing GUI and capturing all data from it.When i click on submit, an xml...; Here is a code that accepts the data from the user through swing
how to create frame in swings
how to create frame in swings  how to create frame in swings
Advertisements
how to create xml schema from xml doc using java
how to create xml schema from xml doc using java  i want to create xml schema from xml document programatically using java... am using Netbeans IDE 7.0 i hav created the xml document for a table of data from Database... now i
How to create one xml file from existing xml file's body?
How to create one xml file from existing xml file's body?  Hi, i'm working with content optimization system.I want to know how we can take all data from an xml doc's body to develope another xml with that content.I'm using JDOm
How to create form in Swings
How to create form in Swings  How to create registration, login and question form in Java Swing?   Creating different forms in Java Swing - Examples how to create registration form in swing swing login form example
How to create XML file - XML
How to create XML file  Creating a XML file, need an example. Thanks... language has many API's to create xml file from program.Here is the code example...()); }}Thanks  Hi,Java programming language has many API's to create xml file from
create a xml from sql server 2005 - XML
create a xml from sql server 2005  hello Dear, i want to know how we create a xml file which retrieve data from Sql server 2005 using java. i... with the name as 'student' i want to create a xml file from the table values which
Display data from xml file to Swings - Swing AWT
Display data from xml file to Swings  Hi, We are Preparing a stand alone application. Where the Swings is the front end. There will be only... clicking the buttons in swings it has to display 20 record in one shot in table
Java Swing Create XML file
Java Swing Create XML file In this tutorial, you will learn how to create XML file using swings. Here is a code that accepts the data from the user through... and fetch all the data from textfields and create an xml file with proper
how to create a xml page
how to create a xml page  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <
create the SQL tables from an XML schema
create the SQL tables from an XML schema  I'm using Eclipse, Please let me know if anyone developped an application that would automatically create the SQL tables from an XML schema
swings:how to link from one form to another form
swings:how to link from one form to another form  how to link from one form to another form
How to generate XML from XSD?
How to generate XML from XSD?  Hi Experts, I have a xsd with me. I want to generate XML files based on the XSD with fields filled out from the database tables. Please Help me out
how to update xml from java - XML
how to update xml from java  hi, Im new to xml parsing and dont know much about. I need to modify the attribute val of a tag in a complex xml file... friend, To solve your problem, please visit to: http://www.roseindia.net/xml
XML and swings - Java Beginners
XML and swings  I have an xml file where all the contents that should be present in the tabbed pane are there. i would like to know how to parse the xml such that, whenever it reads the component type as label, it should create
Create XMl dynamically - XML
Create XMl dynamically  Hi I am retreiving the list from database which i need to display in an XML file with some nodes How can I do
how to create an xml file in following clear format
how to create an xml file in following clear format  anyone please help me out to create this file,,. <Tasks> <Taskid>1 <Taskname>Coding <Project>CeMIC <Date>
How to pretty print XML from Java?
How to pretty print XML from Java?  How to pretty print XML from Java
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
how to create 3d chart in dojox charting - XML
how to create 3d chart in dojox charting  Please provide some example for 3d charting in dojox chart
java code to create xml document from DOM object
java code to create xml document from DOM object  Hey! After knowing the way we can create DOM objects and add elements to it-> then displaying it on the console ;is there a way I can output the same in xml document
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
how to go from one frame to another frame in swings - Java Beginners
how to go from one frame to another frame in swings  Hi, My task is to go from one frame to another.......... let us think that iam having two... program from roseindia.........but, the program is done in applets or swings
How to set the border in header and footer from login page using swings/awt
How to set the border in header and footer from login page using swings/awt  Hi, How to set the border in header and footer from login page using swings/awt
How to access the Title tag from xml to jsp
How to access the Title tag from xml to jsp  How to access the Title tag from xml to jsp   Please visit the following link: http... that will read an xml file and display the data into jsp file
How to access the following tag from xml to jsp
How to access the following tag from xml to jsp  How can i get the title where cat="1" and "My Title" from XML File?   Please visit... will provide you an example that will read an xml file and display the data
How to read value from xml using java?
How to read value from xml using java?  Hi All, I want to read value from following xml using java.. In <Line>,data is in format of key... in advance <pre lang="xml"> &lt;?xml version="1.0" encoding="UTF-8" ?>
How to values from xml using java?
How to values from xml using java?  Hi All, I want to read value from following < Line> xml using java.. In < Line>,data is in format of key and value pair.. i want to read only values..could u plz help me
Create XML - XML
Create XML   Hi, Can you please provide java code,that will create xxx.XML file where all the attributes and values of those attributes is predefined from java file? Thanks in advance Sumanta  Hi friend
How to extract details from XML? - JSP-Servlet
How to extract details from XML?  I want to extract details from http://service.openkapow.com/palanikumar/airportantigua.rest How can i extract details from this link?Here is my program... Airport
How to create a .mdf file from script (.sql)
How to create a .mdf file from script (.sql)  Hi, I have a sql database in 2008 server. now i want it to convert to 2005. so i script it. just help me to create a .mdf file back from the script
How to Create Multiplication Table from 1 to 10?
How to Create Multiplication Table from 1 to 10?  Hi, I want to develop an small application store products. So, i how to create multiplication... table. Thanks   Hi, Did you want to create multiplication table 1
Create XML file from flat file and data insert into database
Create XML file from flat file and data insert into database... have developed an application to create xml file from flat file and data...). Create a file "FlatFileXml.java"  used to create an XML and data
JAXB Create XML File And Get Data From XML
JAXB Create XML File And Get Data From XML In this section we will read about how to create XML file and how to convert XML file's data to Java Object... : Here we are giving a simple example into which we will first create an XML file
How to create file from input values in Jframe ?
How to create file from input values in Jframe ?  hi i m doing my project using java desktop application in netbeans.i designed a form to get the user's academic details and on clicking the submit button,it displays all
How to parse the attributes from XML using KXML parser in j2me
How to parse the attributes from XML using KXML parser in j2me  Dear Sir, I have doubt in How to parse the attributes from XML using KXML... from xml...please help me for parsing the attributes in j2me... Regards Selva
how to create a php script to download file from database mysql
how to create a php script to download file from database mysql  how to create a php script to download file from databse mysql
how to create a php script to download file from database mysql
how to create a php script to download file from database mysql  how to create a php script to download file from databse mysql
How to parse the attributes values from XML using KXML parser in j2me
How to parse the attributes values from XML using KXML parser in j2me  hai team, i did xml parsing from serverside...i got a resultant value from xml,but here tag value name only came from parsing attribute name cant come
how to write in xml? - XML
how to write in xml?  can anybody give the code how to write in xml... Source source = new DOMSource(doc); // Create File to view your xml...(new InputStreamReader(System.in)); System.out.print("Enter XML file name
How to Generate XML files from the data in DB tables?
How to Generate XML files from the data in DB tables?  HI Experts, I have a doubt. I have a table say Pest. My task is to generate XML file with all the fields in table and store it to database.Please help me.I am new to XML
how can we store the encrypted passwaord in swings?
how can we store the encrypted passwaord in swings?   how can we store the encrypted passwaord in swings
JDOM Element Example, How to add and remove child from xml file.
JDOM Element Example, How to add and remove child from xml file. In this tutorial, we will see how to add and remove child node in an xml Document tree. We can add child any where in xml document tree with the help of JDOM API
how to create a bar chart in jsp by fetching value from oracle databse?
how to create a bar chart in jsp by fetching value from oracle databse?  i want to show the population of various states in a bar chart in my jsp page by fetching the data from my oracle table. i am using my eclipse as my IDE
What is XML?
and create well formatted XML document. What is XML Document? Some facts about XML...What is XML? In the first section of XML tutorials we will learn the basics of XML and then write our first xml document. After completing this section
how to create text file from jsp and provide downlode option
how to create text file from jsp and provide downlode option  HI... FROM ROLE AS r INNER JOIN EMPLOYEEMASTER AS e ON r.ROLEID=e.ROLEID WHERE... * from Employee"); %> <script type="text/javascript">
Loading properties from a XML file
Loading properties from a XML file       This Example shows you how to Load properties from a XML... of xml documents. Here the Document BuilderFactory is used to create new DOM
Create XML File using Servlet
Create XML File using Servlet       In this section, you will learn how to create xml... XML file with its version and encoding and display a message 'Xml File Created
how to add audio clip in swings with event handling
how to add audio clip in swings with event handling  hello dear, i wanna to play audio clip in java program in JFrame in swings by event handling... how it can be done...... thanks alot
Advertisements
 

 

 

DMCA.com