import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
public class app extends JFrame{
private JTextField name,age;
private JLabel nameLbl,ageLbl,label;
private JButton Add;
private JPanel pnl1,pnl2,pnl3;
private GridBagConstraints gbc;
public app(){
super("App");
setLayout(new BorderLayout());
//these are the panels which will hold components
pnl1 = new JPanel();
pnl3 = new JPanel();
//they will use the gridBagLayout
final GridBagLayout gridbag = new GridBagLayout();
pnl1.setLayout(gridbag);
gbc = new GridBagConstraints();
nameLbl = new JLabel("Name");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
pnl1.add(nameLbl,gbc);
name = new JTextField(10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 0;
pnl1.add(name,gbc);
ageLbl = new JLabel("Age");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
pnl1.add(ageLbl,gbc);
age = new JTextField(10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 1;
pnl1.add(age,gbc);
label = new JLabel();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
pnl1.add(label,gbc);
Add = new JButton("Add");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 3;
pnl1.add(Add,gbc);
add(pnl1);
writeToXML add = new writeToXML();
Add.addActionListener(add);
}
public class writeToXML implements ActionListener{
public void actionPerformed(ActionEvent add){
if(add.getSource() == Add){
String personName = name.getText();
String personAge = age.getText();
try{
DocumentBuilderFactory docFact = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFact.newDocumentBuilder();
Document doc = docBuilder.newDocument();
//root element
Element rootElement = doc.createElement("people");
doc.appendChild(rootElement);
//device element
Element infoElement = doc.createElement("info");
rootElement.appendChild(infoElement);
//persons name element
Element nameElement = doc.createElement("Name");
nameElement.appendChild(doc.createTextNode(personName));
infoElement.appendChild(nameElement);
//persons age element
Element ageElement = doc.createElement("Age");
ageElement.appendChild(doc.createTextNode(personAge));
infoElement.appendChild(ageElement);
TransformerFactory TransFactory = TransformerFactory.newInstance();
Transformer transformer = TransFactory.newTransformer();
File fp = new File("testing.xml");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(fp);
transformer.transform(source, result);
label.setText("Done");
name.setText("");
age.setText("");
}catch(ParserConfigurationException pce){
pce.printStackTrace();
}catch(TransformerException tfe){
tfe.printStackTrace();
}
}//end of statment if the source is from Add
}//end of actionperformed
}//end of method writeToXMl
public static void main(String args[]){
app Project = new app();
Project.setSize(400, 400);
Project.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Project.setVisible(true);
}//end of main
}