tags:

views:

1443

answers:

3

I am trying to add a "title" element but am getting a NO_MODIFICATION_ALLOWED_ERR error...

 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"));

the line above is throwing error: Exception in thread "main" org.w3c.dom.DOMException: NO_MODIFICATION_ALLOWED_ERR: An attempt is made to modify an object where modifications are not allowed. at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(Unknown Source) at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(Unknown Source) at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source) at myProject.Main.saveDoc(Main.java:171) at myProject.Main.main(Main.java:48) break;

        }



        nextNode = nextNode.getNextSibling();



       }
}

My xml file looks like this:

<?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>
A: 

For some reason, the parent node seems to be read-only. Clone the document by using:

Document newDoc = doc.cloneNode(true);

Set it to read-write by:

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

Then do your stuff. I would return the new document after saving it though.

Bogdan
neither Node nor Document has a setReadOnly property in 1.4. Could you help me with this?
joe
A: 

Where is the original document coming from?

That's the cause of the issue - the code that's reading in the document is constructing a read-only document. Without knowing how you're reading it in, it's pretty hard to work out how to change that.

I just did a quick test on Windows with JDK 1.4.2-11, and I can confirm that using the DocumentBuilderFactory (with the XML content coming from a Reader) does not create a read only Document.

I updated the code so show where I am getting it from.
joe
**updated the code sample to show
joe
+1  A: 

Not sure if that's the reason, but check if your DOM implementation validates all the changes to the DOM. Because in you code,

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

will attempt to create a text node as the child of map element and DITA Map doesn't allow that. Instead, try

Element title = doc.createElement("title");
title.appendChild(doc.createTextNode("title content"))
nextNode.appendChild(title);
jelovirt
Good insight. But I changed the code (and updated the question), same problem occurs.
joe