2008-11-03 4 views
3
나는 "제목"요소를 추가하려고하지만 NO_MODIFICATION_ALLOWED_ERR 오류를 얻고있다

... 자바에서 XML 요소를 추가하는 방법 1.4

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 : 수정이 허용되지 않는 객체를 수정하려고 시도합니다. com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore (알 수없는 출처) com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore (알 수없는 출처) at com. sun.org.apache.xerces.internal.dom.NodeImpl.appendChild (알 수없는 소스) at myProject.Main.saveDoc (Main.java:171) at myProject.Main.main (Main.java:48) 휴식;

   } 



       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> 

답변

2

그 이유가 확실하지 않지만 DOM 구현이 DOM에 대한 모든 변경 사항의 유효성을 검사하는지 확인하십시오. 당신 코드 때문에

nextNode.appendChild(doc.createTextNode("title")); 

map 요소의 자식으로 텍스트 노드를 만들려고 시도하고 DITA 맵은 허용하지 않습니다. 대신 시도해보십시오.

Element title = doc.createElement("title"); 
title.appendChild(doc.createTextNode("title content")) 
nextNode.appendChild(title); 
+0

으로 표시되도록 업데이트되었습니다. 좋은 통찰력. 하지만 코드를 변경 (그리고 질문을 업데이트), 같은 문제가 발생합니다. – joe

0

는 부모 노드는 읽기 전용 것으로 보인다. 사용하여 문서를 복제 :

Document newDoc = doc.cloneNode(true); 

세트는 읽기 - 쓰기를하기로 :

newDoc.setReadOnly(false,true); 
//      ^^^^ also sets children 

가 그런 물건을한다. 저장 한 후에 새 문서를 반환합니다.

+0

노드 또는 문서에는 1.4의 setReadOnly 속성이 없습니다. 이걸 좀 도와 주실 래요? – joe

0

원본 문서의 출처는 어디입니까?

그게 문제의 원인입니다. 문서에서 읽는 코드는 읽기 전용 문서를 구성하고 있습니다. 어떻게 읽는 지 알지 못하면 어떻게 바꿀 수 있습니까?

JDK 1.4.2-11을 사용하여 Windows에서 간단하게 테스트 한 결과 독자가 제공하는 XML 컨텐츠로 DocumentBuilderFactory를 사용하면 읽기 전용 문서가 작성되지 않는다는 것을 확인할 수 있습니다.

+0

코드를 업데이트하여 어디에서 가져 왔는지 보여줍니다. – joe

+0

** 코드 샘플이 – joe

관련 문제