所以由于某种原因,我的 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