编辑 XML 时并发修改错误
我在 JSP 文件中使用 Java 编辑 XML 文件时遇到并发修改错误。这是如何引起的以及如何解决?
ElementFilter f = new ElementFilter("rurl-link");
Iterator subchilditr = childNode.getDescendants(f);
while (subchilditr.hasNext()) { // Exception is thrown here.
Element subchild = (Element) subchilditr.next();
if (subchild.getText().equalsIgnoreCase(prevtext)) {
subchild.setText(link);
out.println("Updated");
}
}
这是堆栈跟踪:
java.util.ConcurrentModificationException
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
java.util.AbstractList$Itr.next(AbstractList.java:343)
org.jdom.DescendantIterator.next(DescendantIterator.java:134)
org.jdom.FilterIterator.hasNext(FilterIterator.java:91)
org.apache.jsp.rurl_005fchangelink_005fxml_jsp._jspService(rurl_005fchangelink_005fxml_jsp.java:101)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I am getting an concurrent modification error while editing an XML file using Java in a JSP file. How is this caused and how can I solve it?
ElementFilter f = new ElementFilter("rurl-link");
Iterator subchilditr = childNode.getDescendants(f);
while (subchilditr.hasNext()) { // Exception is thrown here.
Element subchild = (Element) subchilditr.next();
if (subchild.getText().equalsIgnoreCase(prevtext)) {
subchild.setText(link);
out.println("Updated");
}
}
This is the stacktrace:
java.util.ConcurrentModificationException
java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
java.util.AbstractList$Itr.next(AbstractList.java:343)
org.jdom.DescendantIterator.next(DescendantIterator.java:134)
org.jdom.FilterIterator.hasNext(FilterIterator.java:91)
org.apache.jsp.rurl_005fchangelink_005fxml_jsp._jspService(rurl_005fchangelink_005fxml_jsp.java:101)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您正在使用 JDOM 库?您需要在迭代器循环之外修改
subchild
:I believe you're using the JDOM library? You need to modify
subchild
outside of the iterator loop: