Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: i need help with xml

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default i need help with 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>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
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: i need help with xml

    Hey.
    First of all, I'd just like to mention how I would have done it, although It doesn't mean it would be best or correct way.
    I would have just stored everything into a Collection and then written the Collection to a file in 1 big chunk at the end.

    A very, very quick fix would be to simply use the following code:
    					DOMSource source = new DOMSource(doc);
    					StreamResult result = null;
    					result = new StreamResult(new FileOutputStream(fp, true));
    					transformer.transform(source, result);

    All I've done there is use the OutputStream constructor of StreamResult, and set it into 'append' mode if you like, so It carries on at the end of the file each time.
    The problem with that is that it will also print your XML header each time as well. By no means am I saying that this is the approach you should or shouldn't take, but hopefully It might give you some ideas.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    My Mood
    Nerdy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: i need help with xml

    hey newbie,

    thanks for the quick reply i have added it to my program and it dose add onto it, however a part of what i am also try to do with this project is also read through the xml file, in the gui the user enters the name of a person and the name and age will pop up in a table (that's the plan i have in mind anyway). although this solution dose allow me to constantly add more data, it dose not allow my to read due to the xml header.

    however i was thinking of a way to do it although it might be far to complicated is to have two files, where the program writes into one file and the data is transfare from the first xml file into the second. this might be a bit to complicated and i dont know if its possible, is this what you mean by a collection? or would be collection be an array of some sort?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,297
    Thanks
    180
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: i need help with xml

    From the looks of it it seems for model is recreated new every time you save something - so when you want to add another data element, you rewrite over previous models. Try to read the document from the data file first, then add elements, and finally save the data model.