Results 1 to 1 of 1
Thread: i need help adding data to xml
- 07-23-2011, 10:31 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 1
- Rep Power
- 0
i need help adding data to xml
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>nfnf</Name>
<Age>10</Age>
</info>
</people>
<people>
<info>
<Name>nfnf</Name>
<Age>10</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.
Java Code: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 }
Similar Threads
-
Need help adding data from an array with a variable.
By javaguy345 in forum New To JavaReplies: 10Last Post: 05-22-2011, 05:22 AM -
Linking nodes/adding node data
By falkon114 in forum New To JavaReplies: 0Last Post: 04-19-2011, 09:44 PM -
Adding all elements of an Array together, not adding up correctly
By Teclis in forum New To JavaReplies: 1Last Post: 04-05-2011, 08:58 PM -
Lucene: output elaborated data by adding IR information to it
By aneuryzma in forum LuceneReplies: 0Last Post: 02-22-2011, 10:33 PM -
java project help, reading in from a file and adding data to JTable
By Ekul in forum New To JavaReplies: 0Last Post: 11-24-2009, 01:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks