lundi 3 novembre 2014

In Jsoup how to change/split nodes during iteration


Vote count:

0




I am having html file with document. During parsing i will split textnode which causes Concurrent modification error.




private void processInContent(Node ele) {
String text = "";
for (Node child : ele.childNodes()) {
Node parentNode = child.parentNode();
if (child instanceof TextNode && !("a").equalsIgnoreCase(parentNode.nodeName())) {
TextNode childText = (TextNode) child;
text = childText.text();
System.out.println(text);
Matcher m = pattern.matcher(text);
while (m.find()) {
String matched = null;
boolean url = false;
if (m.group(2) != null) {
matched = m.group(6);

} else {
break;
}
text = childText.text();
TextNode replaceNode = childText.splitText(text.indexOf(matched));
TextNode lastNode = replaceNode.splitText(matched.length());
Element anchorEle = ele.ownerDocument().createElement("a");
anchorEle.attr("href", "mailto:" + matched);

anchorEle.attr("target", "_blank");
anchorEle.text(matched);
replaceNode.replaceWith(anchorEle);
childText = lastNode;
}
}
}
}


Sample Content



<div id="abc"><br>---- The email address is abc@abc.com</b> contains abc
domain email address <br></div>


I want to add anchor tag for email address which results in below exception



java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at java.util.Collections$UnmodifiableCollection$1.next(Collections.java:1008)
at JSOUPParse.processInContent(JSOUPParse.java:253)
at JSOUPParse.main(JSOUPParse.java:318)


Please help me to solve this.



asked 2 mins ago

Roshan

205






In Jsoup how to change/split nodes during iteration

Aucun commentaire:

Enregistrer un commentaire