this is a code for an input window that takes in data from users i tried to save whatever data the user put in in axml file but it is giving me errors...what is wrong with it...help
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package notifyme;
/**
*
* @author hermela
*/
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class InputWindow extends JFrame {
static Container pane;
private JLabel EQUIPMENT;
private JLabel Location;
private JLabel DATE;
private JLabel TIME, TYPE, space, space1;
private JButton enter = new JButton("Enter");
JButton compareBtn = new JButton("Comapre");
private JComboBox location1, equipment1, type1;
private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();
private JTextField time;
JTextArea xmlfileData;
private JSpinner jSpinner1;
//private java.text.SimpleDateFormat tf;
DateComboBox dcb = new DateComboBox();
private static String[] Lnames = {"SelectOne", "Addis Ababa", "Dire Dawa", "Mekelle", "Arbaminch", "Gode", "Gore", "Gawassa"};
private static String[] Elist = {
"SelectOne", "COMMUNICATION", "NAVIGATION", "SURVEILLANCE"};
private static String[] Clist = {
"SelectCommEqui", "VHF121.9", "VHF118.1", "125.1", "125.2", "VSAT"};
private static String[] Nlist = {
"SelectNavEqui", "VOR", "DME", "ILS", "DVOR", "GP", "AB Beacon", "BL Beacon"};
private static String[] Slist = {
"SelectSurveillanceEqui", "PSR", "SSR", "ADS-B",};
SpinnerDateModel sm;
public static void main(String args[]) {
try {
InputWindow iw = new InputWindow();
iw.setSize(400, 400);
} catch (ParseException ex) {
Logger.getLogger(InputWindow.class.getName()).log(Level.SEVERE, null, ex);
}
}
public InputWindow() throws ParseException {
super("Notification Input Window");
setLayout(new FlowLayout());
final Container pane = getContentPane();
pane.setLayout(new GridLayout(8, 8));
EQUIPMENT = new JLabel("EQUIPMENT");
TYPE = new JLabel("TYPE");
equipment1 = new JComboBox(Elist);
SelectType st = new SelectType();
equipment1.addActionListener(st);
//equipment1.addItemListener(this);
type1 = new JComboBox();
//type.addItemListener(this);
String[] subItems1 = {
"SelectCommEqui", "VHF121.9", "VHF118.1", "125.1", "125.2", "VSAT"};
subItems.put(Elist[1], subItems1);
String[] subItems2 = {
"SelectNavEqui", "VOR", "DME", "ILS", "DVOR", "GP", "AB Beacon", "BL Beacon"};
subItems.put(Elist[2], subItems2);
String[] subItems3 = {
"SelectSurveillanceEqui", "PSR", "SSR", "ADS-B",};
subItems.put(Elist[3], subItems3);
// mainComboBox.setSelectedIndex(1);
Location = new JLabel("LOCATION");
location1 = new JComboBox(Lnames);
DATE = new JLabel("DATE");
TIME = new JLabel("TIME");
time = new JTextField();
Date date = new Date();
sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY);
jSpinner1 = new javax.swing.JSpinner(sm);
JSpinner.DateEditor de = new JSpinner.DateEditor(jSpinner1, "HH:mm:ss");
jSpinner1.setEditor(de);
clickEnter enter1 = new clickEnter();
enter.addActionListener(enter1);
space = new JLabel();
space1 = new JLabel();
xmlfileData = new JTextArea();
// enter = new JButton("ENTER");
pane.add(EQUIPMENT);
pane.add(equipment1);
pane.add(TYPE);
pane.add(type1);
pane.add(Location);
pane.add(location1);
pane.add(DATE);
pane.add(dcb);
pane.add(TIME);
pane.add(jSpinner1);
//pane.add(time);
pane.add(space);
pane.add(space1);
pane.add(enter);
pane.add(compareBtn);
pane.add( new JScrollPane( xmlfileData));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1700, 1200);
setVisible(true);
compareBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xMLData.compare();
}
});
equipment1.addItemListener(itemListener);
type1.addItemListener(itemListener);
location1.addItemListener(itemListener);
dcb.addItemListener(itemListener);
sm.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
xMLData.compare();
}
});
}
ItemListener itemListener = new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
xMLData.compare();
}
};
class SelectType implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String item = (String) equipment1.getSelectedItem();
Object o = subItems.get(item);
if (o == null) {
type1.setModel(new DefaultComboBoxModel());
} else {
type1.setModel(new DefaultComboBoxModel((String[]) o));
}
}
}
class clickEnter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
xMLData.saveDataFile();
xMLData.loadXMLData();
//FileOutputStream fos = new FileOutputStream(null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
XMLData xMLData = new XMLData();
final class XMLData {
ArrayList<Data> data = new ArrayList<>();
public XMLData() {
loadXMLData();
}
void loadXMLData() {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
File dir = new File("C:\\Users\\shady\\Documents\\NetBeansProjects\\NotifyMe\\xmldata");
File xmlFile[] = dir.listFiles();
for (int i = 0; i < xmlFile.length; i++) {
Document doc = dBuilder.parse(xmlFile[i]);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("data");
// System.out.println(nList.getLength());
for (int j = 0; j < nList.getLength(); j++) {
Node node = nList.item(j);
NodeList nl = node.getChildNodes();
String eqp = nl.item(1).getTextContent();
String type = nl.item(3).getTextContent();
String loc = nl.item(5).getTextContent();
String date = nl.item(7).getTextContent();
String time = nl.item(9).getTextContent();
Data d = new Data(eqp, type, loc, date, time);
data.add(d);
// System.out.println(d);
}
}
} catch (ParserConfigurationException ex) {
ex.printStackTrace();
} catch (SAXException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
void saveDataFile() {
try {
// System.out.println(sm.getValue());
// System.out.println(sm.getDate());
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy");
// System.out.println(dcb.getSelectedItem());
Date ndt = sdf.parse(dcb.getSelectedItem().toString());
// System.out.println(ndt);
SimpleDateFormat nsdt = new SimpleDateFormat("MM/dd/yyyy");
String nndt = nsdt.format(ndt);
// System.out.println(nndt);
SimpleDateFormat sdft = new SimpleDateFormat("HH:mm");
// System.out.println(sdft.format(sm.getDate()));
//
// String name = equipment1.getSelectedItem().toString() + "_" + type1.getSelectedItem().toString()
// + "_" + location1.getSelectedItem().toString() + "_" + df + ".xml";
String name = "c:\\xmldata\\xmlData.xml";
byte byteData[];
File f = new File(name);
if (f.exists()) {
// FileInputStream fis = new FileInputStream(name);
// byteData = new byte[fis.available()];
// fis.read(byteData);
// fis.close();
//
FileOutputStream fos = new FileOutputStream(name);
//// fos.write(byteData, 0, byteData.length);
//// fos.flush();
//// byte[] newXml = generateXML(false).getBytes();
//// fos.write(newXml, 0, newXml.length);
//// fos.flush();
//// fos.close();
byte b[] = new String("<?xml version=\"1.0\"?>\n").getBytes();
fos.write(b, 0, b.length);
fos.flush();
byte b1[] = "<root>".getBytes();
fos.write(b1, 0, b1.length);
fos.flush();
for (int i = 0; i < data.size(); i++) {
String dd = null;
dd = generateXML(data.get(i));
fos.write(dd.getBytes());
fos.flush();
}
String dd = generateXML();
fos.write(dd.getBytes());
fos.write("</root>".getBytes());
fos.flush();
fos.close();
} else {
FileOutputStream fos = new FileOutputStream(name);
byte b[] = new String("<?xml version=\"1.0\"?>\n").getBytes();
fos.write(b, 0, b.length);
fos.flush();
byte b1[] = "<root>".getBytes();
fos.write(b1, 0, b1.length);
fos.flush();
// System.out.println("root call");
byte[] newXml = generateXML().getBytes();
fos.write(newXml, 0, newXml.length);
fos.write("</root>".getBytes());
fos.flush();
fos.close();
}
// File file=new File(name);
// file.createNewFile();
// FileOutputStream fos = new FileOutputStream(file);
// byte by[]=generateXML(df).getBytes();
// fos.write(by,0,by.length);
// fos.flush();
// fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private String generateXML() {
StringBuilder sb = new StringBuilder();
//sb.append("<?xml version=\"1.0\"?>");
sb.append("\n<data>\n");
sb.append("<equipment>" + equipment1.getSelectedItem().toString() + "</equipment>\n");
sb.append("<type>" + type1.getSelectedItem().toString() + "</type>\n");
sb.append("<location>" + location1.getSelectedItem().toString() + "</location>\n");
sb.append("<date>" + getDate() + "</date>\n");
sb.append("<time>" + getTime() + "</time>\n");
sb.append("</data>");
String st = sb.toString();
// System.out.println(st);
return st;
}
private String generateXML(Data d) {
StringBuilder sb = new StringBuilder();
sb.append("\n<data>\n");
sb.append("<equipment>" + d.getEquipment() + "</equipment>\n");
sb.append("<type>" + d.getType() + "</type>\n");
sb.append("<location>" + d.getLocation() + "</location>\n");
sb.append("<date>" + d.getDate() + "</date>\n");
sb.append("<time>" + d.getTime() + "</time>\n");
sb.append("</data>");
String st = sb.toString();
//System.out.println(st);
return st;
}
String getDate() {
String date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy");
// System.out.println(dcb.getSelectedItem());
Date ndt = sdf.parse(dcb.getSelectedItem().toString());
SimpleDateFormat nsdt = new SimpleDateFormat("MM/dd/yyyy");
date = nsdt.format(ndt);
// System.out.println(date);
} catch (Exception e) {
//e.printStackTrace();
}
return date;
}
String getTime() {
String time = null;
try {
SimpleDateFormat sdft = new SimpleDateFormat("HH:mm");
time = sdft.format(sm.getDate());
//System.out.println(time);
} catch (Exception e) {
//e.printStackTrace();;
}
return time;
}
void compare() {
try {
Data curentData = new Data(
equipment1.getSelectedItem().toString(),
type1.getSelectedItem().toString(),
location1.getSelectedItem().toString(), getDate(), getTime());
// System.out.println(data.size());
for (int i = 0; i < data.size(); i++) {
//System.out.println(data.get(i));
if (data.get(i).equals(curentData)) {
xmlfileData.setText(generateXML());
}
}
} catch (Exception e) {
}
}
}
}