gpt4 book ai didi

java - NO_MODIFICATION_ALLOWED_ERR : An attempt is made to modify an object where modifications are not allowed

转载 作者:行者123 更新时间:2023-12-04 06:27:43 25 4
gpt4 key购买 nike

您好,我在尝试添加时不允许修改
一个新节点到一个 xml 文件,我不知道为什么,因为我对另一个文件使用相同的代码并且它工作正常,这里是代码:

 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 with File object to save to 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);

}

}

我的xml如下:
<?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>

有了这个 DTD:
<!-- DTD for bar stock (drink ingredients -->
<!-- One day we should add a way to allow quantities and units to be included -->

<!ELEMENT stock (item)*>

<!ELEMENT item (#PCDATA)>
<!ATTLIST item quantity CDATA #IMPLIED>

谢谢您的帮助。

编辑:我(Amir Rachum)正在将我自己的堆栈跟踪添加到问题中,因为我也有这个问题:
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 cs236369.hw5.servlets.xml.UploadTheme.addThemeToList(UploadTheme.java:115)
at cs236369.hw5.servlets.xml.UploadTheme.doPost(UploadTheme.java:91)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

最佳答案

@Amir Rachum,@JustMe:

异常的含义:NO_MODIFICATION_ALLOWED_ERR:如果此节点为只读则引发

在上面的代码中,你依赖于 doc.getFirstChild();为您提供根元素。永远不要这样做,因为根据 DOM 模型,文档的子元素是注释、处理指令、根元素。所以你永远不知道你的 DOM 解析器实现可能会给你处理指令或评论。而是使用以下内容:-

 Element rootelment=document.getDocumentElement();
//code to create a new node
rootlement.appendChild(newnode);

如果您仍然不确定您的代码,只需在调用 appendChild() 方法之前打印 getNodeType() 然后查看 DOM specification是否为只读节点。

关于java - NO_MODIFICATION_ALLOWED_ERR : An attempt is made to modify an object where modifications are not allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5840358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com