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.*; public class InimSalvestus extends Panel implements ActionListener{ TextField tf1=new TextField(); TextField tf2=new TextField(); TextField tf3=new TextField(); Button nupp1=new Button("Salvesta"); public InimSalvestus(){ setLayout(new GridLayout(4, 2)); add(new Label("Eesnimi:")); add(tf1); add(new Label("Perekonnanimi:")); add(tf2); add(new Label("Sünniaasta:")); add(tf3); add(nupp1); nupp1.addActionListener(this); } public void actionPerformed(ActionEvent ev){ try{ Document d=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); Element juur=d.createElement("inimesed"); d.appendChild(juur); Element e=d.createElement("inimene"); Element en=d.createElement("eesnimi"); en.appendChild(d.createTextNode(tf1.getText())); e.appendChild(en); Element pn=d.createElement("perekonnanimi"); pn.appendChild(d.createTextNode(tf2.getText())); e.appendChild(pn); Element sa=d.createElement("synniaasta"); sa.appendChild(d.createTextNode(tf3.getText())); e.appendChild(sa); juur.appendChild(e); Transformer t=TransformerFactory.newInstance().newTransformer(); t.transform(new DOMSource(d), new StreamResult(new FileOutputStream("inimesed.xml"))); }catch(Exception ex){ ex.printStackTrace(); } } public static void main(String[] arg){ Frame f=new Frame(); f.add(new InimSalvestus()); f.setSize(400, 400); f.setVisible(true); } }