Skip to content

Commit 12a62dd

Browse files
committed
XML: refactor NodeList -> elements logic
Now there is a static utility method for painlessly extracting a list of Element objects from a given NodeList.
1 parent 0d07a8c commit 12a62dd

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/main/java/org/scijava/util/XML.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,7 @@ public String cdata(final String expression) {
188188

189189
/** Obtains the elements identified by the given XPath expression. */
190190
public ArrayList<Element> elements(final String expression) {
191-
final NodeList nodes = xpath(expression);
192-
final ArrayList<Element> elements = new ArrayList<Element>();
193-
if (nodes != null) {
194-
for (int i=0; i<nodes.getLength(); i++) {
195-
final Node node = nodes.item(i);
196-
if (node instanceof Element) elements.add((Element) node);
197-
}
198-
}
199-
return elements;
191+
return elements(xpath(expression));
200192
}
201193

202194
/** Obtains the nodes identified by the given XPath expression. */
@@ -248,6 +240,18 @@ public static String cdata(final Element el, final String child) {
248240
return cdata(children.item(0));
249241
}
250242

243+
/** Gets the element nodes from the given node list. */
244+
public static ArrayList<Element> elements(final NodeList nodes) {
245+
final ArrayList<Element> elements = new ArrayList<Element>();
246+
if (nodes != null) {
247+
for (int i=0; i<nodes.getLength(); i++) {
248+
final Node node = nodes.item(i);
249+
if (node instanceof Element) elements.add((Element) node);
250+
}
251+
}
252+
return elements;
253+
}
254+
251255
// -- Helper methods --
252256

253257
/** Loads an XML document from the given file. */

0 commit comments

Comments
 (0)