Je suis en train d'ajouter un élément « titre » mais je reçois une erreur de NO_MODIFICATION_ALLOWED_ERR ...Comment ajouter un élément XML en Java 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"));
la ligne ci-dessus est de lancer erreur: Exception en fil "main" org.w3c.dom.DOMException: NO_MODIFICATION_ALLOWED_ERR
: Une tentative est faite pour modifier un objet où les modifications ne sont pas autorisées. at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore (source inconnue) at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore (source inconnue) at com. sun.org.apache.xerces.internal.dom.NodeImpl.appendChild (source inconnue) à myProject.Main.saveDoc (Main.java:171) à myProject.Main.main (Main.java:48) break;
}
nextNode = nextNode.getNextSibling();
}
}
Mon fichier xml ressemble à ceci:
<?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>
Bonne idée. Mais j'ai changé le code (et mis à jour la question), le même problème se produit. – joe