How to add an xml element in Java 1.4

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>


Asked by: Marcus680 | Posted: 28-01-2022






Answer 1

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

Answered by: Connie242 | Posted: 01-03-2022



Answer 2

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.

Answered by: John379 | Posted: 01-03-2022



Answer 3

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.

Answered by: Catherine789 | Posted: 01-03-2022



Similar questions

java - How can I see if an element in an int array is empty?

example: I want to see if array[5] holds a value or is empty.


How to call a java method from a jsp when a html element is clicked?

As title says, how do I call a java class method from a jsp, when certain element is clicked (by example an anchor)? (Without reloading the page) If this can be done, how I pass the method, some part of the html code of the page that invokes it? Im using jsp, servlets, javascript, struts2 and java, over Jboss AS.


java - How to find out an element node with a empty string value in xsl

I am working on transforming a xml file from old version to new version. Here is the basic template which i am using: &lt;xsl:template match="*"&gt; &lt;xsl:element name="{name(.)}" namespace="{namespace-uri(.)}"&gt; &lt;xsl:copy-of select="@*"&gt;&lt;/xsl:copy-of&gt; &lt;xsl:apply-templates&gt;&lt;/xsl:apply-templates&gt; &lt;/xsl:element&gt; &lt;/xsl:template&gt; Howe...


How to check if an element in array exists in Java

If I have an int array structured like this: private int[][] map = new int[400][400]; And I try to retrieve map[100][200] And that element isn't initialized, will i get a compiler/runtime error or will it return null? And is there any function to check if a given element/index exists/has been set?


Remove element from linked list JAVA

OK here is my problem. I have a linked list of card objects. I have the following method public void removeCard(Card card){ cards.remove(card); } if I create Card c = new Card(5,C); for example and there is a card with the exact same values 2, and C in the linked list (cards). If I call the method CardPile.remove(card) I dont get any errors, but t...


string - Java: Get last element after split

I am using the String split method and I want to have the last element. The size of the Array can change. Example: String one = "Düsseldorf - Zentrum - Günnewig Uebachs" String two = "Düsseldorf - Madison" I want to split the above Strings and get the last item: lastone = one.split("-")[here the last item] // &lt;- how? lasttwo = two.split("-")[h...


java - What is the syntax to check for element present using Style in IE

I have the following line that works for Firefox assertTrue(!selenium.isElementPresent("//input[@name=\""+chosen.getField().getName()+"\" and contains(@style, \"color: rgb(255, 0, 0);\")]")); But Fails in IE. When i inspect the field in IE, i see the color style represented in hexadecimal. How would you represent the line above to work on IE?


java - How to assign unique DOM element ID

My GWT application creates text areas, each of which must have an ID in order to be useful to a third-party JavaScript library. I know how to assign an ID to a GWT widget; I'm after a good way of generating those unique ID's.


How to find the index of an element in an array in Java?

I am looking to find the index of a given element, knowing its contents, in Java. I tried the following example, which does not work: class masi { public static void main( String[] args ) { char[] list = {'m', 'e', 'y'}; // should print 1 System.out.println(list[] == "e"); } } Can anyone please explain what is wrong with this ...


In java to remove an element in an array can you set it to null?

I am trying to make a remove method that works on an array implementation of a list. Can I set the the duplicate element to null to remove it? Assuming that the list is in order. ArrayList a = new ArrayList[]; public void removeduplicates(){ for(a[i].equals(a[i+1]){ a[i+1] = null; } a[i+1] = a[i]; }






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top