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

时间:2015-01-20 00:12:30

标签: 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

1 个答案:

答案 0 :(得分:1)

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

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

使用绝对路径,例如

C:\\Temp\\Calendar.xml

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

通过更多的调查,我发现项目根目录中第一次运行的输出,而输入是在资源文件夹中。从那以后,我猜你正在将文件保存在除你期望之外的某个地方。