hello everybody,
i am new to java, i am doing a project which i need help with, my project is a gui application where i write the name and age of a person into this app and that info is then added to and xml file. so far i have been able to create an xml file and this is the result;
<people>
<info>
<Name>nfnf</Name>
<Age>10</Age>
</info>
</people>
but when i try to add more to get this result;
people>
<info>
<Name>david</Name>
<Age>20</Age>
</info>
</people>
<people>
<info>
<Name>john</Name>
<Age>21</Age>
</info>
</people>
the first one disappears and is replaced by the second one, what i want to do is be able to create the result above but with much more data.i have been trying to find work this out for two weeks now and have searched google many times but no luck. so any help would be great. here is the code that i have below. thanks in advance.
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 }