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 >
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);
}
}
Related Tutorials/Questions & Answers:
How to create XML from SwingsHow 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
Advertisements
how to create xml schema from xml doc using javahow 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 form in SwingsHow 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
Display data from xml file to Swings - Swing AWTDisplay 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
create a xml from sql server 2005 - XMLcreate 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
How to create XML file - XMLHow 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 Java Swing Create XML fileJava 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
create the SQL tables from an XML schemacreate 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
how to create a xml pagehow 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">
<
java code to create xml document from DOM objectjava 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
XML and swings - Java BeginnersXML 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 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 - XMLhow 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 Create XMl dynamically - XMLCreate 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 access the Title tag from xml to jspHow 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 jspHow 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">
<?
xml version="1.0" encoding="UTF-8" ?>
Create XML - XMLCreate 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 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
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
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
JAXB Create XML File And Get Data From XMLJAXB
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... to retrieve the data back
from XML to Java content we will
create a Java
class
How to extract details from XML? - JSP-ServletHow 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 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 create pdf from jsongridcreate pdf
from jsongrid i need to
create pdf
from jsongrid in java struts2.. otherwise i need to
create pdf
from result set
Create PDF from javaCreate PDF
from java Good afternoon,
I have a problem
how to
create report. i want to
create report in pdf file
from my database in mysql. Now i... code to
create pdf file
from database call
from java programming.
thank you
SWINGSSWINGS WHAT ARE THE DIFFERENCES BETWEEN AWT AND
SWINGS questions from an xml filequestions
from an
xml file i am developing online bit by bit exam for that i retrieved questions
from an
xml file but when i retrieved using jsp i am getting all questions at a time in a single page.but i want to show one
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