Java Swing Create XML file


 

Java Swing Create XML file

In this tutorial, you will learn how to create XML file using swings.

In this tutorial, you will learn how to create XML file using swings.

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 swing components and display it in xml file. We have created textfields on frame to accept the data from the user. A button is there to perform an action and fetch all the data from textfields and create an xml file with proper binding for all the elements.

Example:

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);
}
}

Ads