문제
"제목"요소를 추가하려고하지만 no_modification_allowed_err 오류를 받고 있습니다 ...
private static void saveDoc(String f) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
// create DOMSource for source XML document
DOMSource xmlSource = new DOMSource(doc);
Node nextNode = xmlSource.getNode().getFirstChild();
while (nextNode != null)
{
System.out.print("\n node name: " + nextNode.getNodeName() + "\n");
if (nextNode.getNodeName().equals("map")){
nextNode.appendChild(doc.createElement("title"));
위의 줄은 오류가 발생합니다. 스레드의 예외 "main"org.w3c.dom.domexception : NO_MODIFICATION_ALLOWED_ERR
: 수정이 허용되지 않는 객체를 수정하려고 시도합니다. at com.sun.org.apache.xerces.internal.dom.internalinsertbefore (알 수없는 소스) (com.sun.org.apache.xerces.internal.dom.parentnode.insertbefore (com.sun.org)의 (알 수없는 출처) at com.sun.org. .apache.xerces.internal.dom.nodeimpl.pendchild (myproject.main.main) (main.java:48) break의 myProject.main.savedoc (main.java:171)의 ApendChild (알 수없는 출처);
}
nextNode = nextNode.getNextSibling();
}
}
내 XML 파일은 다음과 같습니다.
<?xml version="1.0" encoding="UTF-8"?>
<?dctm xml_app="LOPackage"?>
<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "file:C:/Documents%20and%20Settings/joe/Desktop//LOPackage/map.dtd">
<map xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" class="- map/map " ditaarch:DITAArchVersion="1.1" domains="(map mapgroup-d) (topic indexing-d)">
<topicref class="- map/topicref " href="dctm://ai/0501869e80002504?DMS_OBJECT_SPEC=RELATION_ID" type="Le"/>
<topicref class="- map/topicref " href="dctm://ai/0501869e80002505?DMS_OBJECT_SPEC=RELATION_ID" type="Pr"/>
<topicref class="- map/topicref " href="dctm://ai/0501869e80002506?DMS_OBJECT_SPEC=RELATION_ID" type="Pr"/>
</map>
해결책
그것이 이유인지 확실하지 않지만 DOM 구현이 DOM의 모든 변경 사항을 확인하는지 확인하십시오. 코드에서
nextNode.appendChild(doc.createTextNode("title"));
자식으로 텍스트 노드를 만들려고합니다. map
요소와 DITA지도는 그것을 허용하지 않습니다. 대신 시도하십시오
Element title = doc.createElement("title");
title.appendChild(doc.createTextNode("title content"))
nextNode.appendChild(title);
다른 팁
어떤 이유로, 상위 노드는 읽기 전용 인 것 같습니다. 다음을 사용하여 문서를 복제하십시오.
Document newDoc = doc.cloneNode(true);
다음과 같이 읽기로 설정하십시오.
newDoc.setReadOnly(false,true);
// ^^^^ also sets children
그런 다음 당신의 물건을하십시오. 저장 한 후 새 문서를 반환 할 것입니다.
원본 문서는 어디에서 왔습니까?
이것이 문제의 원인입니다. 문서에서 읽는 코드는 읽기 전용 문서를 구성하는 것입니다. 당신이 그것을 어떻게 읽고 있는지 모르면 그것을 바꾸는 방법을 알아내는 것은 매우 어렵습니다.
방금 JDK 1.4.2-11로 Windows에서 빠른 테스트를 수행했으며 (독자에서 나온 XML 컨텐츠와 함께) DocumentBuilDerfactory를 사용하면 읽기 전용 문서를 작성하지 않음을 확인할 수 있습니다.