The generic superclass for nodes that have children. Not counting subclasses, there are exactly two such classes in XOM:
Document
Element
This class provides methods to add and remove child nodes.
300301302303304305306307308309310311312313314315316317
for (int i = 0; i < nodes.size(); i++) { result.append(nodes.get(i)); } } else { ParentNode parent = (ParentNode) parents.get(parents.size()-1); for (int i = 0; i < nodes.size(); i++) { Node node = nodes.get(i); if (node instanceof Attribute) { ((Element) parent).addAttribute((Attribute) node); } else { parent.appendChild(node); } } } }
307308309310311312313314315316317318319320321
replaceByChildren(node, node); } } public static void replaceByChildren(Element toReplace, Element addChildren) { ParentNode parent = toReplace.getParent(); int pos = parent.indexOf(toReplace); Elements toInsert = addChildren.getChildElements(); for (int j = 0; j < toInsert.size(); j++) { Element child = toInsert.get(j); child.detach(); parent.insertChild(child, ++pos); } parent.removeChild(toReplace); }
319320321322323324325326327328329330331332
} parent.removeChild(toReplace); } public static void replace(Node replaced, List<Node> replacements) { ParentNode parent = replaced.getParent(); int pos = parent.indexOf(replaced); parent.removeChild(replaced); if(pos==0) pos = -1; for (Node node : replacements) { node.detach(); parent.insertChild(node, ++pos); } }
363364365366367368369370371372373
/** * Retrieve the nearest ancestor element that has the given attribute, * or null if no such ancestor exists. */ public static Element getAncestorWithAttribute(Element element, String attrName) { ParentNode parent = element.getParent(); if(parent!=null && parent instanceof Element) { Element eparent = (Element)parent; if(eparent.getAttribute(attrName)!=null){ return eparent; }
379380381382383384385386387388389
/** * Retrieve the nearest ancestor element that has the given name, * or null if no such ancestor exists. */ public static Element getAncestor(Element element, String localName, String namespaceURI) { ParentNode parent = element.getParent(); if(parent!=null && parent instanceof Element) { Element eparent = (Element)parent; if(eparent.getLocalName().equals(localName) && eparent.getNamespaceURI().equals(namespaceURI)){ return eparent;
396397398399400401402403404405406
public static void replace(Element toReplace, Element with) { toReplace.getParent().replaceChild(toReplace, with); } public static boolean hasAncestor(Element element, Element possibleAncestor) { ParentNode parent = element.getParent(); if(parent!=null && parent instanceof Element) { Element eparent = (Element)parent; if(eparent == possibleAncestor) { return true; }
199200201202203204205206207208209210211
throw new FunctionCallException(pe); } } public Object getDocumentNode(Object o) { ParentNode parent = null; if (o instanceof ParentNode) { parent = (ParentNode)o; } else if (o instanceof Node) { parent = ((Node)o).getParent(); } return parent.getDocument(); }
384385386387388389390391392393394
if (! isElement(o)) { return JaxenConstants.EMPTY_ITERATOR; } Map nsMap = new HashMap(); Element elt = (Element)o; ParentNode parent = elt; while (parent instanceof Element) { elt = (Element)parent; String uri = elt.getNamespaceURI(); String prefix = elt.getNamespacePrefix();