-1

在 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)
4

1 回答 1

0

我相信您正在使用 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);         
}
于 2011-03-25T01:15:46.320 回答