As it is said on so many forums that element is a special case of node in DOM.
But I got an exception that violate this rule.
It throws exception at statement, elem.remove().
Here, ele is an element. remove() is a function in Jsoup API, that removes nodes and their descendants from DOM.
Exception :-
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalArgumentException: Object must not be null
at org.jsoup.helper.Validate.notNull(Validate.java:16)
at org.jsoup.nodes.Node.remove(Node.java:266)
at XXX.YYY.ZZZ.Template_Matching.Template_Matching.removeProductLister(Template_Matching.java:80)
at XXX.YYY.ZZZ.Template_Matching.Template_Matching.main(Template_Matching.java:376)
... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
Code :-
public static void function(Document doc1, Document doc2, String tag) {
//Checking for ULs
Elements uls_1 = doc1.getElementsByTag(tag);
Elements uls_2 = doc2.getElementsByTag(tag);
for (Element elem1 : uls_1) {
// Check if elem1 exists in DOM, If No, then continue
for (Element elem2 : uls_2) {
// Check if elem2 exists in DOM, If No, then continue
// If id matches, remove them
if ((!"".equals(elem1.id())) && (elem1.id().equals(elem2.id()))) {
elem1.remove();
elem2.remove();
break;
}
}
}
}