编辑 XML 时并发修改错误

发布于 2024-10-25 23:39:23 字数 1314 浏览 6 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆消瘦 2024-11-01 23:39:23

我相信您正在使用 JDOM 库?您需要在迭代器循环之外修改 subchild

ElementFilter f = new ElementFilter("rurl-link");
Iterator subchilditr = childNode.getDescendants(f);
List<Element> subchildList = new ArrayList<Element>();

while (subchilditr.hasNext()) {
    Element subchild = (Element) subchilditr.next();

    if (subchild.getText().equalsIgnoreCase(prevtext)) {
        subchildList.add(subchild);
    }
}

for (Element subchild : subchildList) {
    subchild.setText(link);         
}

I believe you're using the JDOM library? You need to modify subchild outside of the iterator loop:

ElementFilter f = new ElementFilter("rurl-link");
Iterator subchilditr = childNode.getDescendants(f);
List<Element> subchildList = new ArrayList<Element>();

while (subchilditr.hasNext()) {
    Element subchild = (Element) subchilditr.next();

    if (subchild.getText().equalsIgnoreCase(prevtext)) {
        subchildList.add(subchild);
    }
}

for (Element subchild : subchildList) {
    subchild.setText(link);         
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文