Java(将数据添加到xml文件而不覆盖)

时间:2016-03-26 00:22:06

标签: java xml

我正在练习Java Swing,我正在做一个菜单项,将数据保存在xml上,添加内容而不覆盖...问题是,似乎任何错误它总是删除所有xml除了root的第一个子项,然后正确附加数据...

这是xml(带有测试数据)

<?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> 

这是java代码

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);

¿任何想法为什么它似乎删除除第一个之外的所有其他孩子然后追加?

由于

0 个答案:

没有答案