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

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

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

Node.insertBefore介绍

[英]Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.
If newChild is a DocumentFragment object, all of its children are inserted, in the same order, before refChild. If the newChild is already in the tree, it is first removed.

Note: Inserting a node before itself is implementation dependent.
[中]在现有子节点refChild之前插入节点newChild。如果refChildnull,请在子项列表的末尾插入newChild
如果newChildDocumentFragment对象,则其所有子对象都将按相同顺序插入refChild之前。如果newChild已在树中,则首先将其删除。
注意:在节点之前插入节点取决于实现。

代码示例

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

@Override
public org.w3c.dom.Node insertBefore(org.w3c.dom.Node newChild, org.w3c.dom.Node refChild) throws DOMException {
  return node.insertBefore(newChild, refChild);
}

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

/** If the child node doesn't exist, it is created. */
private static Node getFirstChildNodeByName (Node parent, String child) {
  NodeList childNodes = parent.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(child)) {
      return childNodes.item(i);
    }
  }
  Node newNode = parent.getOwnerDocument().createElement(child);
  if (childNodes.item(0) != null)
    return parent.insertBefore(newNode, childNodes.item(0));
  else
    return parent.appendChild(newNode);
}

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

/** If the child node doesn't exist, it is created. */
private static Node getFirstChildNodeByName (Node parent, String child) {
  NodeList childNodes = parent.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(child)) {
      return childNodes.item(i);
    }
  }
  Node newNode = parent.getOwnerDocument().createElement(child);
  if (childNodes.item(0) != null)
    return parent.insertBefore(newNode, childNodes.item(0));
  else
    return parent.appendChild(newNode);
}

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

/** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
 * getFirstChildByAttrValue(properties, "property", "name"); */
private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  NodeList childNodes = node.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(childName)) {
      NamedNodeMap attributes = childNodes.item(i).getAttributes();
      Node attribute = attributes.getNamedItem(attr);
      if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
    }
  }
  Node newNode = node.getOwnerDocument().createElement(childName);
  NamedNodeMap attributes = newNode.getAttributes();
  Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  nodeAttr.setNodeValue(value);
  attributes.setNamedItem(nodeAttr);
  if (childNodes.item(0) != null) {
    return node.insertBefore(newNode, childNodes.item(0));
  } else {
    return node.appendChild(newNode);
  }
}

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

/** If the child node or attribute doesn't exist, it is created. Usage example: Node property =
 * getFirstChildByAttrValue(properties, "property", "name"); */
private static Node getFirstChildByNameAttrValue (Node node, String childName, String attr, String value) {
  NodeList childNodes = node.getChildNodes();
  for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i).getNodeName().equals(childName)) {
      NamedNodeMap attributes = childNodes.item(i).getAttributes();
      Node attribute = attributes.getNamedItem(attr);
      if (attribute.getNodeValue().equals(value)) return childNodes.item(i);
    }
  }
  Node newNode = node.getOwnerDocument().createElement(childName);
  NamedNodeMap attributes = newNode.getAttributes();
  Attr nodeAttr = node.getOwnerDocument().createAttribute(attr);
  nodeAttr.setNodeValue(value);
  attributes.setNamedItem(nodeAttr);
  if (childNodes.item(0) != null) {
    return node.insertBefore(newNode, childNodes.item(0));
  } else {
    return node.appendChild(newNode);
  }
}

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

/**
 * adds processing instruction node to DOM.
 */
public void processingInstruction(String target, String data) {
final Node last = (Node)_nodeStk.peek();
ProcessingInstruction pi = _document.createProcessingInstruction(
  target, data);
if (pi != null){
   if (last == _root && _nextSibling != null)
     last.insertBefore(pi, _nextSibling);
   else
     last.appendChild(pi);
   
   _lastSibling = pi;
  }
}

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

/**
 * Lexical Handler method to create comment node in DOM tree.
 */
public void comment(char[] ch, int start, int length) {
final Node last = (Node)_nodeStk.peek();
Comment comment = _document.createComment(new String(ch,start,length));
if (comment != null){
   if (last == _root && _nextSibling != null)
     last.insertBefore(comment, _nextSibling);
   else
     last.appendChild(comment);
   
   _lastSibling = comment;
  }
}

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

/**
 * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
 * 
 * @param newNode
 *            new node
 * @param refNode
 *            ref node
 * @throws DOMException
 *             DOMException
 */
public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  Node parent = refNode.getParentNode();
  Node next = refNode.getNextSibling();
  if (next == null) {
    parent.appendChild(newNode);
  } else {
    parent.insertBefore(newNode, next);
  }
}

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

/**
 * Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
 * 
 * @param newNode
 *            new node
 * @param refNode
 *            ref node
 * @throws DOMException
 *             DOMException
 */
public static void insertAfter(Node newNode, Node refNode) throws DOMException {
  Node parent = refNode.getParentNode();
  Node next = refNode.getNextSibling();
  if (next == null) {
    parent.appendChild(newNode);
  } else {
    parent.insertBefore(newNode, next);
  }
}

代码示例来源: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: 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: languagetool-org/languagetool

private static Source mergeIntoSource(InputStream baseXmlStream, InputStream xmlStream) throws Exception {
 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
 domFactory.setIgnoringComments(true);
 domFactory.setValidating(false);
 domFactory.setNamespaceAware(true);
 DocumentBuilder builder = domFactory.newDocumentBuilder();
 Document baseDoc = builder.parse(baseXmlStream);
 Document ruleDoc = builder.parse(xmlStream);
 // Shall this be more generic, i.e. reuse not just unification ???
 NodeList unificationNodes = baseDoc.getElementsByTagName("unification");
 Node ruleNode = ruleDoc.getElementsByTagName("rules").item(0);
 Node firstChildRuleNode = ruleNode.getChildNodes().item(1);
 for (int i = 0; i < unificationNodes.getLength(); i++) {
  Node unificationNode = ruleDoc.importNode(unificationNodes.item(i), true);
  ruleNode.insertBefore(unificationNode, firstChildRuleNode);
 }
 return new DOMSource(ruleDoc);
}

代码示例来源: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);
  }
}

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

public void characters(char[] ch, int start, int length) {
final Node last = (Node)_nodeStk.peek();
// No text nodes can be children of root (DOM006 exception)
  if (last != _document) {
    final String text = new String(ch, start, length);
    if( _lastSibling != null && _lastSibling.getNodeType() == Node.TEXT_NODE ){
       ((Text)_lastSibling).appendData(text);
    }
    else if (last == _root && _nextSibling != null) {
      _lastSibling = last.insertBefore(_document.createTextNode(text), _nextSibling);
    }
    else {
      _lastSibling = last.appendChild(_document.createTextNode(text));
    }
    
  }
}

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

private static String styleXML(Node dxfsNode, Node tableStyleNode) throws IOException, TransformerException {
  // built-ins doc uses 1-based dxf indexing, Excel uses 0 based.
  // add a dummy node to adjust properly.
  dxfsNode.insertBefore(dxfsNode.getOwnerDocument().createElement("dxf"), dxfsNode.getFirstChild());
  StringBuilder sb = new StringBuilder();
  sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n")
      .append("<styleSheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" ")
      .append("xmlns:mc=\"http://schemas.openxmlformats.org/markup-compatibility/2006\" ")
      .append("xmlns:x14ac=\"http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac\" ")
      .append("xmlns:x16r2=\"http://schemas.microsoft.com/office/spreadsheetml/2015/02/main\" ")
      .append("mc:Ignorable=\"x14ac x16r2\">\n");
  sb.append(writeToString(dxfsNode));
  sb.append(writeToString(tableStyleNode));
  sb.append("</styleSheet>");
  return sb.toString();
}

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

/**
   * Enclose the elements' closest common ancestor.
   * 
   * @param first
   *            first
   * @param last
   *            last
   */
  protected void slowDown(Element first, Element last) {
    Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY);
    phonol.setAttribute("precision", "precise");
    Document doc = phonol.getOwnerDocument();
    Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY);
    prosody.setAttribute("rate", "-20%");
    phonol.getParentNode().insertBefore(prosody, phonol);
    prosody.appendChild(phonol);
  }
}

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

/**
   * Enclose the elements' closest common ancestor.
   * 
   * @param first
   *            first
   * @param last
   *            last
   */
  protected void slowDown(Element first, Element last) {
    Element phonol = MaryDomUtils.encloseNodesWithNewElement(first, last, MaryXML.PHONOLOGY);
    phonol.setAttribute("precision", "precise");
    Document doc = phonol.getOwnerDocument();
    Element prosody = MaryXML.createElement(doc, MaryXML.PROSODY);
    prosody.setAttribute("rate", "-20%");
    phonol.getParentNode().insertBefore(prosody, phonol);
    prosody.appendChild(phonol);
  }
}

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

private static void appendNodes(Node self, Closure c) {
  Node parent = self.getParentNode();
  Node beforeNode = self.getNextSibling();
  DOMBuilder b = new DOMBuilder(self.getOwnerDocument());
  Element newNodes = (Element) b.invokeMethod("rootNode", c);
  Iterator<Node> iter = XmlGroovyMethods.iterator(children(newNodes));
  while (iter.hasNext()) {
    parent.insertBefore(iter.next(), beforeNode);
  }
}

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

/**
 * Splits this CDATA node into parts that do not contain a "]]>" sequence.
 * Any newly created nodes will be inserted before this node.
 */
public void split() {
  if (!needsSplitting()) {
    return;
  }
  Node parent = getParentNode();
  String[] parts = getData().split("\\]\\]>");
  parent.insertBefore(new CDATASectionImpl(document, parts[0] + "]]"), this);
  for (int p = 1; p < parts.length - 1; p++) {
    parent.insertBefore(new CDATASectionImpl(document, ">" + parts[p] + "]]"), this);
  }
  setData(">" + parts[parts.length - 1]);
}

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

public final Text splitText(int offset) throws DOMException {
  Text newText = document.createTextNode(
      substringData(offset, getLength() - offset));
  deleteData(0, offset);
  Node refNode = getNextSibling();
  if (refNode == null) {
    getParentNode().appendChild(newText);
  } else {
    getParentNode().insertBefore(newText, refNode);
  }
  return this;
}

相关文章