您好我在尝试添加时无法进行修改 一个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)
答案 0 :(得分:3)
@Amir Rachum,@ JustMe:
异常的含义:NO_MODIFICATION_ALLOWED_ERR:如果此节点是只读的,则引发
在上面的代码中,您依靠doc.getFirstChild();
为您提供根元素。从不这样做,因为根据DOM模型,文档的子项是Comment,Processing Instruction,root Element。因此,您永远不会知道您的DOM解析器实现可能会给您处理指令或注释。而是使用以下: -
Element rootelment=document.getDocumentElement();
//code to create a new node
rootlement.appendChild(newnode);
如果您仍然不确定您的代码,只需在调用appendChild()方法之前打印getNodeType(),然后查看DOM specification是否是只读节点。