Refine search
public static void removeChildren(Node e) { NodeList list = e.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node n = list.item(i); e.removeChild(n); } } private static void getMatchingNodes(Node node, Pattern[] nodePath, int cur, List<Node> res) {
public static Node replaceNode(Node self, Closure c) { if (self.getParentNode() instanceof Document) { throw new UnsupportedOperationException("Replacing the root node is not supported"); } appendNodes(self, c); self.getParentNode().removeChild(self); return self; }
/***** * Deletes all the node from the document which match the definition provided by xmlEntity * */ public static void deleteNode(Document doc, XmlEntity xmlEntity) throws Exception { NodeList nodes = getNodes(doc, xmlEntity); if (nodes != null) { int length = nodes.getLength(); for (int i = 0; i < length; i++) { Node node = nodes.item(i); node.getParentNode().removeChild(node); } } }
/** Deletes the option at index. */ @Override public void remove(int index){ NodeList options=getOptions(); Node option=options.item(index);//may be null if(option==null)return; Node parent=option.getParentNode(); parent.removeChild(option); }
public static void removeDirectChildren( Node parent ) { NodeList childNodes = parent.getChildNodes(); while ( childNodes.getLength() > 0 ) { parent.removeChild( childNodes.item( 0 ) ); } }
private void removeEmptyNodes( NodeList nodes ) { for ( int i = 0; i < nodes.getLength(); i++ ) { Node node = nodes.item( i ); // Process the tree bottom-up if ( node.hasChildNodes() ) { removeEmptyNodes( node.getChildNodes() ); } boolean nodeIsEmpty = node.getNodeType() == Node.ELEMENT_NODE && !node.hasAttributes() && !node.hasChildNodes() && node.getTextContent().length() == 0; if ( nodeIsEmpty ) { // We shifted elements left, do not increment counter node.getParentNode().removeChild( node ); i--; } } } }
if ("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) { currentNode.getParentNode().removeChild(currentNode);
public static void collectPatterns(Document document) { NodeList nl = document.getElementsByTagName("pattern"); while(nl.getLength() > 0) { Element pt = (Element) nl.item(0); String id = pt.getAttribute("id"); patterns.put(id, pt); pt.getParentNode().removeChild(pt); } }
thereIsMore = false; for (int i = 0; i < timedNodes.getLength(); i++) { Node p = timedNodes.item(i); long startTime = getStartTime(p); long endTime = getEndTime(p); Node parent = p.getParentNode(); parent.removeChild(p); } else { changeTime(p, "begin", -segmentStartTime);
private void removeLeafNodes(List<Node> candidates) { boolean changed = true; while (changed) { changed = false; Iterator<Node> iterator = candidates.iterator(); while (iterator.hasNext()) { Node node = iterator.next(); if (isLeafNode(node)) { node.getParentNode().removeChild(node); iterator.remove(); changed = true; } } } }
public static void replaceElement(Element oldElement, NodeList newNodes) { Document doc = oldElement.getOwnerDocument(); Node parent = oldElement.getParentNode(); int len = newNodes.getLength(); for (int i = 0; i < len; i++) { Node n = newNodes.item(i); if (!doc.equals(n.getOwnerDocument())) { // first we need to import the node into the document n = doc.importNode(n, true); } parent.insertBefore(n, oldElement); } parent.removeChild(oldElement); }
/** * Recursively apply includes through all SQL fragments. * @param source Include node in DOM tree * @param variablesContext Current context for static variables with values */ private void applyIncludes(Node source, final Properties variablesContext, boolean included) { if (source.getNodeName().equals("include")) { Node toInclude = findSqlFragment(getStringAttribute(source, "refid"), variablesContext); Properties toIncludeContext = getVariablesContext(source, variablesContext); applyIncludes(toInclude, toIncludeContext, true); if (toInclude.getOwnerDocument() != source.getOwnerDocument()) { toInclude = source.getOwnerDocument().importNode(toInclude, true); } source.getParentNode().replaceChild(toInclude, source); while (toInclude.hasChildNodes()) { toInclude.getParentNode().insertBefore(toInclude.getFirstChild(), toInclude); } toInclude.getParentNode().removeChild(toInclude); } else if (source.getNodeType() == Node.ELEMENT_NODE) { NodeList children = source.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { applyIncludes(children.item(i), variablesContext, included); } } else if (included && source.getNodeType() == Node.TEXT_NODE && !variablesContext.isEmpty()) { // replace variables ins all text nodes source.setNodeValue(PropertyParser.parse(source.getNodeValue(), variablesContext)); } }
import java.io.File; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.*; import org.w3c.dom.*; public class Demo { public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); Document document = dbf.newDocumentBuilder().parse(new File("input.xml")); XPathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]"); Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE); b13Node.getParentNode().removeChild(b13Node); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(System.out)); } }
public static void replaceElement(Element oldElement, NodeList newNodes) { Document doc = oldElement.getOwnerDocument(); Node parent = oldElement.getParentNode(); int len = newNodes.getLength(); for (int i = 0; i < len; i++) { Node n = newNodes.item(i); if (!doc.equals(n.getOwnerDocument())) { // first we need to import the node into the document n = doc.importNode(n, true); } parent.insertBefore(n, oldElement); } parent.removeChild(oldElement); }
writer.getDomConfig().setParameter("xml-declaration", false); int iMax = sections.getLength(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < iMax; i++) { final Node item = sections.item(i); item.getParentNode().removeChild(item); iMax = bodies.getLength(); for (int i = 0; i < iMax; i++) { final Node item = bodies.item(i); sb.append(writer.writeToString(item)); iMax = footnotes.getLength(); for (int i = 0; i < iMax; i++) { final Node item = footnotes.item(i); sb.append(writer.writeToString(item));
public static org.w3c.dom.Node appendChild(Node node, org.w3c.dom.Node newChild) throws DOMException { if (node instanceof Branch) { Branch branch = (Branch) node; org.w3c.dom.Node previousParent = newChild.getParentNode(); if (previousParent != null) { previousParent.removeChild(newChild); } branch.add((Node) newChild); return newChild; } throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, "Children not allowed for this node: " + node); }
boolean removeDuplicateGatewayReceivers(Document document) throws TransformerException { boolean modified = false; NodeList receiverNodes = document.getElementsByTagName("gateway-receiver"); while (receiverNodes.getLength() > 1) { Element receiverElement = (Element) receiverNodes.item(0); receiverElement.getParentNode().removeChild(receiverElement); logger.info("Removed duplicate cluster configuration gateway-receiver element=" + XmlUtils.prettyXml(receiverElement)); modified = true; receiverNodes = document.getElementsByTagName("gateway-receiver"); } return modified; }
toInclude = source.getOwnerDocument().importNode(toInclude, true); source.getParentNode().replaceChild(toInclude, source); while (toInclude.hasChildNodes()) { toInclude.getParentNode().insertBefore(toInclude.getFirstChild(), toInclude); toInclude.getParentNode().removeChild(toInclude); } else if (source.getNodeType() == Node.ELEMENT_NODE) { if (included && !variablesContext.isEmpty()) { for (int i = 0; i < children.getLength(); i++) { applyIncludes(children.item(i), variablesContext, included);
public Object getPayload(JAXBContext arg0) { try { Source s = getPayload(); if (s instanceof DOMSource) { DOMSource ds = (DOMSource)s; ds.setNode(org.apache.cxf.helpers.DOMUtils.getDomElement(ds.getNode())); Node parent = ds.getNode().getParentNode(); Node next = ds.getNode().getNextSibling(); if (parent instanceof DocumentFragment) { parent.removeChild(ds.getNode()); } try { return JAXBUtils.unmarshall(arg0, ds); } finally { if (parent instanceof DocumentFragment) { parent.insertBefore(ds.getNode(), next); } } } return JAXBUtils.unmarshall(arg0, getPayload()); } catch (JAXBException e) { throw new WebServiceException(e); } }
public static void combineAllApplyTags(Document document) { NodeList nl = document.getElementsByTagName("apply"); while(nl.getLength() > 0) { Element app = (Element) nl.item(0); Element parent = (Element) app.getParentNode(); NamedNodeMap attrs = app.getAttributes(); for(int i = 0; i < attrs.getLength(); i++) { Node ns = attrs.item(i); parent.setAttribute(ns.getNodeName(), ns.getNodeValue()); } while(app.getChildNodes().getLength() > 0) { Node ni = app.getChildNodes().item(0); app.getParentNode().insertBefore(ni, app); } app.getParentNode().removeChild(app); } }