org.w3c.dom.Node.removeChild()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(9.6k)|赞(0)|评价(0)|浏览(279)

本文整理了Java中org.w3c.dom.Node.removeChild()方法的一些代码示例,展示了Node.removeChild()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Node.removeChild()方法的具体详情如下:
包路径:org.w3c.dom.Node
类名称:Node
方法名:removeChild

Node.removeChild介绍

[英]Removes the child node indicated by oldChild from the list of children, and returns it.
[中]从子节点列表中删除由oldChild指示的子节点,并将其返回。

代码示例

代码示例来源:origin: stanfordnlp/CoreNLP

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) {

代码示例来源:origin: groovy/groovy-core

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;
}

代码示例来源:origin: apache/geode

/*****
 * 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);
  }
 }
}

代码示例来源:origin: cat.inspiracio/html-parser

/** 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);
}

代码示例来源:origin: simpligility/android-maven-plugin

public static void removeDirectChildren( Node parent )
{
  NodeList childNodes = parent.getChildNodes();
  while ( childNodes.getLength() > 0 )
  {
    parent.removeChild( childNodes.item( 0 ) );
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

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--;
   }
  }
 }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

if ("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) {
  currentNode.getParentNode().removeChild(currentNode);

代码示例来源:origin: osmandapp/Osmand

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);
  }
  
}

代码示例来源:origin: sannies/mp4parser

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);

代码示例来源:origin: spring-projects/spring-restdocs

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;
      }
    }
  }
}

代码示例来源:origin: marytts/marytts

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);
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * 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));
 }
}

代码示例来源:origin: stackoverflow.com

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));
  }

}

代码示例来源:origin: marytts/marytts

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);
}

代码示例来源:origin: vsch/flexmark-java

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));

代码示例来源:origin: org.dom4j/dom4j

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);
}

代码示例来源:origin: apache/geode

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;
}

代码示例来源:origin: org.mybatis/mybatis

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);

代码示例来源:origin: org.apache.cxf/cxf-rt-frontend-jaxws

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);
  }
}

代码示例来源:origin: osmandapp/Osmand

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);
  }
}

相关文章