public Node replaceNode(Closure c) { if (size() <= 0 || size() > 1) { throw new GroovyRuntimeException( "replaceNode() can only be used to replace a single node, but was applied to " + size() + " nodes"); } return ((Node)get(0)).replaceNode(c); }
/** * Creates a new NodeList containing the same elements as the * original (but cloned in the case of Nodes). * * @return the clone */ @Override public Object clone() { NodeList result = new NodeList(size()); for (int i = 0; i < size(); i++) { Object next = get(i); if (next instanceof Node) { Node n = (Node) next; result.add(n.clone()); } else { result.add(next); } } return result; }
NodeList nodeList = doc.getElementsByTagName("Morning"); Node node = nodeList.get("sunrise"); String sunrise = node.getValue();
@Nullable private static Node getFirstChild(Node xml, String field) { if (xml==null) return null; NodeList nl = (NodeList)xml.get(field); if (nl==null || nl.isEmpty()) return null; return (Node)nl.get(0); } @Nonnull
public Node replaceNode(Closure c) { if (size() <= 0 || size() > 1) { throw new GroovyRuntimeException("replaceNode() can only be used to replace a single node."); } return ((Node)get(0)).replaceNode(c); }
/** * Selects correct path for multiple variants. If possible path node has assigned id, different with current * child id, then node can't be used. Only not used node selected (without id at all or with same id). * * @param list possible source paths * @param child child node to find path for * @return resolved path or null (in null case child should be simply appended to source xml) */ @Nullable private static Node findPath(NodeList list, Node child) { String childID = nid(child); Node firstNidNull = null; // look for matching id or any node without id for (int i = 0, size = list.size(); i < size; i++) { Node node = (Node) list.get(i); if (childID.equals(nid(node))) { return node; } if (firstNidNull == null && nid(node) == null) { firstNidNull = node; } } return firstNidNull; }