java - 是什么阻止我的 XML 文件更新?

标签 java xml file-writing

由于某种原因,我的 XML 文件不会更新。我很确定我已经正确设置了所有内容,因为同样的事情在其他地方也适用。我已经包含了一个 for 循环,用于在删除节点后立即打印出节点的名称,并且它可以正确打印所有内容。我还尝试向我正在尝试编辑的节点添加一个测试节点,并且它也可以正确打印。

这是java文件:

package xmlpractice;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class XMLPractice {
    private DocumentBuilderFactory documentFactory;
    private DocumentBuilder documentBuilder;
    private Document xmlDoc;
    private Node rootNode;
    private static Node calendarDataNode;

    private static int month_index = 1;

    public XMLPractice() {
        try {
            documentFactory = DocumentBuilderFactory.newInstance();
            documentBuilder = documentFactory.newDocumentBuilder();
            xmlDoc = documentBuilder.parse(XMLPractice.class.getResourceAsStream("Calendar.xml"));
            rootNode = xmlDoc.getDocumentElement();
            calendarDataNode = rootNode.getChildNodes().item(1);
        } catch(ParserConfigurationException | SAXException | IOException e) {e.printStackTrace(System.out);}      

        doClick("test");
    }

    public void doClick(String tf_label) {
         for (int y=0; y<calendarDataNode.getChildNodes().getLength(); y++) {
            //if (year node name == "y" + current year)
            if (calendarDataNode.getChildNodes().item(y).getNodeName().equals("y" + 2015)) {
                //for (int m=0; m<number of child nodes in year node; m++)
                for (int m=0; m<calendarDataNode.getChildNodes().item(y).getChildNodes().getLength(); m++) {
                    //if (month node name == "m" + current month)
                    if (calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getNodeName().equals("m" + (month_index-1))) {
                        //for (int d=0; d<number of child nodes in month node; d++)
                        for (int d=1; d<calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().getLength(); d+=2) {
                            //if (day node name == "d" + current day)
                            if (calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getNodeName().equals("d" + 1)) {

                                //Added test node to see if it would work correctly. Prints out correctly in for loop, but does the same thing.
                                Element newNode = xmlDoc.createElement("lTest");
                                newNode.setTextContent("testttttt");

                                //Prints correct node I'm trying to edit
                                System.out.println(calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getNodeName());

                                calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).appendChild(newNode);

                                //for (int l=0; l<number of child nodes in day node; l++)
                                for (int l=1; l<calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().getLength(); l++) {
                                    //if (label text contents == String label)
                                    if (calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().item(l).getTextContent().equals(tf_label)) {

                                        //Prints name of correct node I'm trying to edit.
                                        System.out.println(calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().item(l).getNodeName() + "\n");
                                        calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).removeChild(calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().item(l));

                                        //Printing out node names to see if they were removed.
                                        for (int i=0; i<calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().getLength(); i++) {
                                            System.out.println(calendarDataNode.getChildNodes().item(y).getChildNodes().item(m).getChildNodes().item(d).getChildNodes().item(i).getNodeName());
                                        }
                                    }
                                }
                            }
                        }    
                    }
                }
            }
        }

        OutputFormat outF = new OutputFormat(xmlDoc);

        try (FileOutputStream outStream = new FileOutputStream("Calendar.xml")) {
            XMLSerializer serializer = new XMLSerializer(outStream, outF);
            serializer.serialize(xmlDoc);

            outStream.close();
        }catch(IOException e) {e.printStackTrace(System.out);}
    }

    public static void main(String args[]) {
       new XMLPractice();
    } 
}

这是 XML 文件:

<root>
    <calendar-data>
        <y2015>
            <m0>
                <d1>
                    <l0>test</l0> //This should be removed
                    <l1>TEST</l1> 
                    //lTest should be added
                </d1>
            </m0>
        </y2015>
    </calendar-data>
</root>

这是输出:

d1
l0

test
#text //l0 is missing
#text
l1
#text
lTest //lTest is added

最佳答案

我下载并运行了您提供的代码,仅添加了一个示例 public static void main(String[] args) 来运行该应用程序。正如您所描述的,它没有更新源 XML 文件。更换您的

FileOutputStream outStream = new FileOutputStream("Calendar.xml")

使用绝对路径,例如

C:\\Temp\\Calendar.xml

它按预期工作,输出到提供的绝对路径。

经过更多调查,我发现第一次运行的输出位于项目根目录中,而输入位于资源文件夹中。由此看来,我猜测您将文件保存在您期望的位置之外的位置。

关于java - 是什么阻止我的 XML 文件更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28035755/

相关文章:

java - 文件名java中的特殊字符

xml - 创建 'flexible' XML 架构

java - HttpServletRequest 的 getRequestURL() 不返回确切的 URL

java - JTextField setEnabled 与 setEditable

java - 字符串到单链表

android - 二进制 XML 文件行 #2 : Error inflating class fragment

xml - PHP SimpleXML xpath : contains and position

java - 在 Java 中将 XML 文件转换为 JSON 文件的最快方法是什么?

Python:将 Numpy 数组写入具有浮点精度的文件

java - 静态工厂 builder 的麻烦