Skip to content

Commit 6738b96

Browse files
committed
XML: expose cdata extraction as a utility method
It is helpful for painlessly extracting CDATA from a Node.
1 parent 1d690c2 commit 6738b96

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public Document getDocument() {
181181
public String cdata(final String expression) {
182182
final NodeList nodes = xpath(expression);
183183
if (nodes == null || nodes.getLength() == 0) return null;
184-
return getCData(nodes.item(0));
184+
return cdata(nodes.item(0));
185185
}
186186

187187
/** Obtains the nodes identified by the given XPath expression. */
@@ -212,6 +212,20 @@ public String toString() {
212212
}
213213
}
214214

215+
// -- Utility methods --
216+
217+
/** Gets the CData beneath the given node. */
218+
public static String cdata(final Node item) {
219+
final NodeList children = item.getChildNodes();
220+
if (children == null || children.getLength() == 0) return null;
221+
for (int i = 0; i < children.getLength(); i++) {
222+
final Node child = children.item(i);
223+
if (child.getNodeType() != Node.TEXT_NODE) continue;
224+
return child.getNodeValue();
225+
}
226+
return null;
227+
}
228+
215229
// -- Helper methods --
216230

217231
/** Loads an XML document from the given file. */
@@ -252,18 +266,6 @@ private static DocumentBuilder createBuilder()
252266
return DocumentBuilderFactory.newInstance().newDocumentBuilder();
253267
}
254268

255-
/** Gets the CData beneath the given node. */
256-
private static String getCData(final Node item) {
257-
final NodeList children = item.getChildNodes();
258-
if (children == null || children.getLength() == 0) return null;
259-
for (int i = 0; i < children.getLength(); i++) {
260-
final Node child = children.item(i);
261-
if (child.getNodeType() != Node.TEXT_NODE) continue;
262-
return child.getNodeValue();
263-
}
264-
return null;
265-
}
266-
267269
/** Converts the given DOM to a string. */
268270
private static String dumpXML(final Document doc)
269271
throws TransformerException

0 commit comments

Comments
 (0)