I am practicing with Java Swing and I am doing a menu item that saves the data on a xml, adding the content without overwriting... the matter is that it seems that for any mistake it always delete all the xml except the first child of root, and then appends the data correctly...
Here is the xml (with test data)
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Settings> <Setting> <Name>r</Name> <Channel>r</Channel> <Amp>r</Amp> <Effects>r</Effects> <Pregain>r</Pregain> <Low>r</Low> <Mid>r</Mid> <High>r</High> <Postgain>r</Postgain> <Param1>r</Param1> <Param2>r</Param2> <Feedback>r</Feedback> <Level>r</Level> <Reverb>r</Reverb> </Setting> <Setting> <Name>d</Name> <Channel>d</Channel> <Amp>d</Amp> <Effects>d</Effects> <Pregain>d</Pregain> <Low>d</Low> <Mid>d</Mid> <High>d</High> <Postgain>d</Postgain> <Param1>d</Param1> <Param2>d</Param2> <Feedback>d</Feedback> <Level>d</Level> <Reverb>d</Reverb> </Setting> </Settings> And here is the java code
menuitemsave = new JMenuItem("Save"); menuitemsave.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // Read XML Document document = docBuilder.parse("Settings.xml"); // Root - Settings Element root = document.getDocumentElement(); // Element - Setting Element Setting = document.createElement("Setting"); root.appendChild(Setting); // Attribute - Name Element Name = document.createElement("Name"); Name.appendChild(document.createTextNode(txtname.getText())); Setting.appendChild(Name); // Attribute - Channel Element Channel = document.createElement("Channel"); Channel.appendChild(document.createTextNode(txtchannel.getText())); Setting.appendChild(Channel); // Attribute - Amp Element Amp = document.createElement("Amp"); Amp.appendChild(document.createTextNode(txtamp.getText())); Setting.appendChild(Amp); // Attribute - Effects Element Effects = document.createElement("Effects"); Effects.appendChild(document.createTextNode(txteffects.getText())); Setting.appendChild(Effects); // Attribute - Pre Gain Element Pregain = document.createElement("Pregain"); Pregain.appendChild(document.createTextNode(txtpregain.getText())); Setting.appendChild(Pregain); // Attribute - Low Element Low = document.createElement("Low"); Low.appendChild(document.createTextNode(txtlow.getText())); Setting.appendChild(Low); // Attribute -Mid Element Mid = document.createElement("Mid"); Mid.appendChild(document.createTextNode(txtmid.getText())); Setting.appendChild(Mid); // Attribute -High Element High = document.createElement("High"); High.appendChild(document.createTextNode(txthigh.getText())); Setting.appendChild(High); // Attribute -Post Gain Element Postgain = document.createElement("Postgain"); Postgain.appendChild(document.createTextNode(txtpostgain.getText())); Setting.appendChild(Postgain); // Attribute -Parameter 1 Element Param1 = document.createElement("Param1"); Param1.appendChild(document.createTextNode(txtparam1.getText())); Setting.appendChild(Param1); // Attribute -Parameter 2 Element Param2 = document.createElement("Param2"); Param2.appendChild(document.createTextNode(txtparam2.getText())); Setting.appendChild(Param2); // Attribute -Feedback Element Feedback = document.createElement("Feedback"); Feedback.appendChild(document.createTextNode(txtfeedback.getText())); Setting.appendChild(Feedback); // Attribute -Level Element Level = document.createElement("Level"); Level.appendChild(document.createTextNode(txtlevel.getText())); Setting.appendChild(Level); // Attribute -Reverb Element Reverb = document.createElement("Reverb"); Reverb.appendChild(document.createTextNode(txtreverb.getText())); Setting.appendChild(Reverb); // Write the content into XML file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File("Settings.xml")); transformer.transform(source, result); JOptionPane.showMessageDialog(null, "Saved"); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } catch (SAXException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); mnMenu.add(menuitemsave); ¿Any idea about why it seems to delete all the other childs except the first and then append?
Thanks
No comments:
Post a Comment