따라서 어떤 이유로 내 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
의도 한대로 작동하여 제공된 절대 경로로 출력되었습니다.
좀 더 조사를 해보니 프로젝트 루트에서 처음 실행 한 결과를 찾았지만 입력은 리소스 폴더에있었습니다. 그로부터 예상했던 곳이 아닌 다른 곳에 파일을 저장하고 있다고 생각합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다