How to add an xml element in Java 1.4

I am trying to add a title element, but getting the error 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")); 

the above line throws an error: An exception in the stream "main" org.w3c.dom.DOMException:: NO_MODIFICATION_ALLOWED_ERRan attempt is made to modify the object if 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) on myProject.Main.saveDoc (Main.java:171) on myProject.Main.main (Main.java:48) break;

              }



              nextNode = nextNode.getNextSibling();



          }
}

My xml file is as follows:

<?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>
+3
source share
3

, , , DOM DOM.

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

node map, DITA Map .

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

- node . , :

Document newDoc = doc.cloneNode(true);

:

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

. , .

0

?

- , . , , , .

Windows JDK 1.4.2-11, , DocumentBuilderFactory ( XML, Reader) .

0

Source: https://habr.com/ru/post/1699082/


All Articles