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 Nimesisestus{ public static void main(String[] arg) throws Exception{ Document d=null; Element juur=null; String failinimi="eesnimed.xml"; if(new File(failinimi).exists()){ d=DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(failinimi); juur=(Element)d.getFirstChild(); } else { d=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); juur=d.createElement("eesnimed"); d.appendChild(juur); } BufferedReader klaver=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Sisesta eesnimed, l6petuseks x"); String sisend=klaver.readLine(); while(!sisend.equals("x")){ Element e=d.createElement("eesnimi"); e.appendChild(d.createTextNode(sisend)); juur.appendChild(e); sisend=klaver.readLine(); } Transformer t=TransformerFactory.newInstance().newTransformer(); OutputStream valjund=new FileOutputStream(failinimi); t.transform(new DOMSource(d), new StreamResult(valjund)); valjund.close(); } }