【问题标题】:Appending to an XML file while checking current nodes在检查当前节点时附加到 XML 文件
【发布时间】:2014-07-05 18:28:03
【问题描述】:

我到处搜索这个问题,但无法弄清楚发生了什么。

我要做的是将一些内容附加到 XML 文件的末尾,同时检查节点以确保其中没有该节点。因此,如果我想添加到 XML 文件但已经存在,则不会写入。

我遇到的问题是它可以很好地附加到末尾,但是无论节点是否存在,它都会执行此操作。因此,如果我运行 5 次,我最终会让它附加相同的东西五次而不是一次。我的代码如下:

    public void writeSettingsFile(String file, String keyIn, String contentIn)
{
    try {
        String filepath = file;
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setIgnoringComments(true);
        docFactory.setIgnoringElementContentWhitespace(true);
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);

        Node settings = doc.getElementsByTagName("Settings").item(0);

        NodeList list = settings.getChildNodes();

        Element newSetting = doc.createElement(keyIn);
        newSetting.appendChild(doc.createTextNode(contentIn));

        for (int i = 0; i < list.getLength(); i++)
        {
            Node node = list.item(i);
            System.out.println(i);
            System.out.println(node.getNodeName());

            if (!keyIn.equals(node.getNodeName()) && !hasWhiteSpace(node))
            {
                settings.appendChild(newSetting);
                System.out.println("This should be written");
                System.out.println("Not written");
            }
            else
            {
                System.out.println("Not written"); 
            }
        }

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);

    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
       } catch (TransformerException tfe) {
        tfe.printStackTrace();
       } catch (IOException ioe) {
        ioe.printStackTrace();
       } catch (SAXException sae) {
        sae.printStackTrace();
       }
}

【问题讨论】:

    标签: java xml append exists


    【解决方案1】:

    好的,我想通了,我稍微改变了循环,我创建了一个布尔变量来保存是否找到键。默认设置为 false,它会扫描密钥,如果找到它然后将 keyFound 更改为 true,则不会写入我们的新密钥,如果没有找到它将保持为 false,这将写入我们的密钥。代码贴在下面。

                Boolean keyFound = false;
            for (int i = 0; i < list.getLength(); i++)
            {
                Node node = list.item(i);
                System.out.println(i);
                System.out.println(node.getNodeName());
    
                if (keyIn.equals(node.getNodeName()))
                {
    
                    keyFound = true;
                    System.out.println("Found");
                }
            }
            System.out.println("All keys scanned");
            if (keyFound == false)
            {
                settings.appendChild(newSetting);
                System.out.println("Written");
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      相关资源
      最近更新 更多