java - NO_MODIFICATION_ALLOWED_ERR: An attempt is made to modify an object where modifications are not allowed -
hello getting no modification allowed when trying add new node xml file , not sure why because using same code file , works fine, here code:
public void addstockitem(string itemstr, int qty){ string path = system.getproperty("user.dir") + "/src/stock.xml"; try { documentbuilderfactory docfactory = documentbuilderfactory.newinstance(); docfactory.setignoringelementcontentwhitespace(true); documentbuilder docbuilder; docbuilder = docfactory.newdocumentbuilder(); document doc = docbuilder.parse(path); node root = doc.getfirstchild(); node item = doc.createelement("item"); item.settextcontent(itemstr); namednodemap itemattr = item.getattributes(); attr qtyattr = doc.createattribute("quantity"); qtyattr.settextcontent(qty+""); itemattr.setnameditem(qtyattr); root.appendchild(item); transformer transformer = transformerfactory.newinstance().newtransformer(); transformer.setoutputproperty(outputkeys.indent, "yes"); //initialize streamresult file object save file streamresult result = new streamresult(new stringwriter()); domsource source = new domsource(doc); transformer.transform(source, result); string xmlstring = result.getwriter().tostring(); bufferedwriter out = new bufferedwriter(new filewriter(path)); out.write(xmlstring); out.close(); } catch (exception ex) { system.out.print(ex); } }
my xml following:
<?xml version="1.0" encoding="utf-8"?> <!doctype stock system "barstock.dtd"> <stock> <item quantity="23">dark rum</item> <item quantity="5">light rum</item> <item quantity="4">vodka</item> <item quantity="2">brandy</item> <item quantity="3">orange juice</item> <item quantity="2">cream</item> <item quantity="2">dry vermouth</item> <item quantity="7">amaretto</item> </stock>
with dtd:
<!-- dtd bar stock (drink ingredients --> <!-- 1 day should add way allow quantities , units included --> <!element stock (item)*> <!element item (#pcdata)> <!attlist item quantity cdata #implied>
thanks help.
edit: (amir rachum) adding own stacktrace question have problem well:
org.w3c.dom.domexception: no_modification_allowed_err: attempt made modify object modifications not allowed. @ com.sun.org.apache.xerces.internal.dom.parentnode.internalinsertbefore(unknown source) @ com.sun.org.apache.xerces.internal.dom.parentnode.insertbefore(unknown source) @ com.sun.org.apache.xerces.internal.dom.nodeimpl.appendchild(unknown source) @ cs236369.hw5.servlets.xml.uploadtheme.addthemetolist(uploadtheme.java:115) @ cs236369.hw5.servlets.xml.uploadtheme.dopost(uploadtheme.java:91) @ javax.servlet.http.httpservlet.service(httpservlet.java:637) @ javax.servlet.http.httpservlet.service(httpservlet.java:717) @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:290) @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:206) @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:233) @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:191) @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:127) @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:102) @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:109) @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:298) @ org.apache.coyote.http11.http11processor.process(http11processor.java:859) @ org.apache.coyote.http11.http11protocol$http11connectionhandler.process(http11protocol.java:588) @ org.apache.tomcat.util.net.jioendpoint$worker.run(jioendpoint.java:489) @ java.lang.thread.run(unknown source)
@amir rachum , @justme:
meaning of exception:no_modification_allowed_err: raised if node readonly
in above code, relied on doc.getfirstchild();
provide root element. never that, because per dom model, children of document comment, processing instruction, root element. never know dom parser implementation might giving processing instruction or comment. instead use following:-
element rootelment=document.getdocumentelement(); //code create new node rootlement.appendchild(newnode);
if still not sure ur code, print getnodetype() before calling appendchild() method on , dom specification whether readonly node or not.
Comments
Post a Comment