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

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

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

Node.cloneNode介绍

[英]Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent ( parentNode is null) and no user data. User data associated to the imported node is not carried over. However, if any UserDataHandlers has been specified along with the associated data these handlers will be called with the appropriate parameters before this method returns.
Cloning an Element copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes, but this method does not copy any children it contains unless it is a deep clone. This includes text contained in an the Element since the text is contained in a child Text node. Cloning an Attr directly, as opposed to be cloned as part of an Element cloning operation, returns a specified attribute (specified is true). Cloning an Attr always clones its children, since they represent its value, no matter whether this is a deep clone or not. Cloning an EntityReference automatically constructs its subtree if a corresponding Entity is available, no matter whether this is a deep clone or not. Cloning any other type of node simply returns a copy of this node.
Note that cloning an immutable subtree results in a mutable copy, but the children of an EntityReference clone are readonly . In addition, clones of unspecified Attr nodes are specified. And, cloning Document, DocumentType, Entity, and Notation nodes is implementation dependent.
[中]返回此节点的副本,即用作节点的通用副本构造函数。重复节点没有父节点(parentNodenull),也没有用户数据。未结转与导入节点关联的用户数据。但是,如果指定了任何UserDataHandlers以及相关数据,则在该方法返回之前,将使用适当的参数调用这些处理程序。
克隆Element会复制所有属性及其值,包括由XML处理器生成以表示默认属性的属性,但此方法不会复制它包含的任何子级,除非它是深度克隆。这包括Element中包含的文本,因为该文本包含在子Text节点中。直接克隆Attr而不是作为Element克隆操作的一部分进行克隆,会返回指定的属性(specifiedtrue)。克隆Attr始终克隆其子项,因为它们代表其价值,无论这是否是深度克隆。如果对应的Entity可用,则克隆EntityReference会自动构造其子树,无论这是否是深度克隆。克隆任何其他类型的节点只会返回此节点的副本。
请注意,克隆不可变子树会产生可变副本,但EntityReference克隆的子级是只读的。此外,还指定了未指定Attr节点的克隆。而且,克隆DocumentDocumentTypeEntityNotation节点取决于实现。

代码示例

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

@Override
public org.w3c.dom.Node cloneNode(boolean deep) {
  return node.cloneNode(deep);
}

代码示例来源:origin: plutext/docx4j

/**
 * Adds a new line to the DOM.
 * 
 * @throws DOMException If thrown by method invoked on the underlying DOM document
 */
private void newLine() {
 this.currentElement.appendChild(this.newline.cloneNode(false));
}

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

protected boolean replaceNode(Node[] primaryNodes, Node testNode, List<Node> usedNodes) {
    boolean foundItem = false;
    for (int j=0;j<primaryNodes.length;j++){
      if (primaryNodes[j].getNodeName().equals(testNode.getNodeName())) {
        Node newNode = primaryNodes[j].getOwnerDocument().importNode(testNode.cloneNode(true), true);
        primaryNodes[j].getParentNode().replaceChild(newNode, primaryNodes[j]);
        usedNodes.add(testNode);
        foundItem = true;
      }
    }

    return foundItem;
  }
}

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

public Node[] merge(List<Node> nodeList1, List<Node> nodeList2, List<Node> exhaustedNodes) {
  if (CollectionUtils.isEmpty(nodeList1) || CollectionUtils.isEmpty(nodeList2)) {
    return null;
  }
  Node node1 = nodeList1.get(0);
  Node node2 = nodeList2.get(0);
  NodeList list2 = node2.getChildNodes();
  for (int j = 0; j < list2.getLength(); j++) {
    node1.appendChild(node1.getOwnerDocument().importNode(list2.item(j).cloneNode(true), true));
  }
  Node[] response = new Node[nodeList2.size()];
  for (int j = 0; j < response.length; j++) {
    response[j] = nodeList2.get(j);
  }
  return response;
}

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

public static void applyPatterns(Document document) {
  NodeList nl = document.getElementsByTagName("apply");
  for (int i = 0; i < nl.getLength();) {
    Element app = (Element) nl.item(i);
    String pt = app.getAttribute("pattern");
    if (!pt.equals("")) {
      if (!patterns.containsKey(pt)) {
        throw new IllegalStateException("Pattern '" + pt + "' is not defined");
      }
      Element patt = patterns.get(pt);
      final NodeList pattChildren = patt.getChildNodes();
      for(int ki = 0; ki < pattChildren.getLength(); ki++) {
        Node ni = patt.getChildNodes().item(ki);
        app.getParentNode().insertBefore(ni.cloneNode(true), app);
      }
      app.getParentNode().removeChild(app);
    } else {
      i++;
    }
  }
}

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

@Override
public Node[] merge(List<Node> nodeList1, List<Node> nodeList2, List<Node> exhaustedNodes) {
  if (CollectionUtils.isEmpty(nodeList1) || CollectionUtils.isEmpty(nodeList2)) {
    return null;
  }
  Node node1 = nodeList1.get(0);
  Node node2 = nodeList2.get(0);
  NamedNodeMap attributes2 = node2.getAttributes();
  Comparator<Object> nameCompare = new Comparator<Object>() {
    @Override
    public int compare(Object arg0, Object arg1) {
      return ((Node) arg0).getNodeName().compareTo(((Node) arg1).getNodeName());
    }
  };
  Node[] tempNodes = {};
  tempNodes = exhaustedNodes.toArray(tempNodes);
  Arrays.sort(tempNodes, nameCompare);
  int length = attributes2.getLength();
  for (int j = 0; j < length; j++) {
    Node temp = attributes2.item(j);
    int pos = Arrays.binarySearch(tempNodes, temp, nameCompare);
    if (pos < 0) {
      Attr clone = (Attr) temp.cloneNode(true);
      ((Element) node1).setAttributeNode((Attr) node1.getOwnerDocument().importNode(clone, true));
    }
  }
  return null;
}

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

protected boolean replaceNode(Node[] primaryNodes, Node testNode, final String attribute, List<Node> usedNodes) {
  if (testNode.getAttributes().getNamedItem(attribute) == null) {
    return false;
  }
  Node[] filtered = NodeUtil.filterByAttribute(primaryNodes, attribute);
  int pos = NodeUtil.findNode(filtered, testNode, attribute, true);
  if (pos >= 0) {
    Node foundNode = filtered[pos];
    Node newNode = foundNode.getOwnerDocument().importNode(testNode.cloneNode(true), true);
    foundNode.getParentNode().replaceChild(newNode, foundNode);
    usedNodes.add(testNode);
    return true;
  }
  return false;
}

代码示例来源:origin: org.netbeans.api/org-openide-util

/**
 * Convert an XML fragment from one namespace to another.
 * 
 * @param from element to translate
 * @param namespace namespace to be translated to
 * @return
 * 
 * @since 8.4
 */
public static Element translateXML(Element from, String namespace) {
  Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName());
  NodeList nl = from.getChildNodes();
  int length = nl.getLength();
  for (int i = 0; i < length; i++) {
    Node node = nl.item(i);
    Node newNode;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
      newNode = translateXML((Element) node, namespace);
    } else {
      newNode = node.cloneNode(true);
    }
    to.appendChild(newNode);
  }
  NamedNodeMap m = from.getAttributes();
  for (int i = 0; i < m.getLength(); i++) {
    Node attr = m.item(i);
    to.setAttribute(attr.getNodeName(), attr.getNodeValue());
  }
  return to;
}

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

Node newNode = ownerDocument.importNode(node.cloneNode(true), true);
parentNode.appendChild(newNode);
usedNodes.add(node);

代码示例来源:origin: org.netbeans.api/org-openide-util

newNode = node.cloneNode(true);
newNode = to.getOwnerDocument().importNode(newNode, true);

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

Node node1Parent = nodeList1.get(0).getParentNode();
for (Node aNodeList2 : nodeList2) {
  Node tempNode = node1Parent.getOwnerDocument().importNode(aNodeList2.cloneNode(true), true);
  if (LOG.isDebugEnabled()) {
    StringBuffer sb = new StringBuffer();

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

Node foundNode = filtered[pos];
Node targetNode = foundNode.getOwnerDocument().importNode(foundNode.cloneNode(false), false);
Node newTestNode = foundNode.getOwnerDocument().importNode(testNode.cloneNode(true), true);
NodeUtil.mergeNodeLists(targetNode, newTestNode.getChildNodes(), foundNode.getChildNodes(), "name");
foundNode.getParentNode().replaceChild(targetNode, foundNode);

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

break evaluate;
Node newNode = filtered[pos].getOwnerDocument().importNode(testNode.cloneNode(true), true);
filtered[pos].getParentNode().replaceChild(newNode, filtered[pos]);

代码示例来源:origin: plutext/docx4j

appendSelf(contextNodes.item(i).cloneNode(true));

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

Node singleNode = result.item(i).cloneNode(true);

代码示例来源:origin: apache/servicecomb-java-chassis

public static void mergeElement(Element from, Element to) {
  // attrs
  for (int idx = 0; idx < from.getAttributes().getLength(); idx++) {
   Node node = from.getAttributes().item(idx);
   to.getAttributes().setNamedItem(node.cloneNode(false));
  }

  // children
  for (int idx = 0; idx < from.getChildNodes().getLength(); idx++) {
   Node node = from.getChildNodes().item(idx);

   if (!Element.class.isInstance(node)) {
    continue;
   }

   to.appendChild(node.cloneNode(true));
  }
 }
}

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

/**
 * Compares two DOM {@link Node nodes} by comparing the representations of
 * the nodes as XML strings
 * 
 * @param node1 the first node
 * @param node2 the second node
 * @return true if the XML representation node1 is the same as the XML
 *         representation of node2, otherwise false
 */
public static boolean compareNodes(Node node1, Node node2) {
 Validate.notNull(node1, "First node required");
 Validate.notNull(node2, "Second node required");
 // The documents need to be cloned as normalization has side-effects
 node1 = node1.cloneNode(true);
 node2 = node2.cloneNode(true);
 // The documents need to be normalized before comparison takes place to
 // remove any formatting that interfere with comparison
 if (node1 instanceof Document && node2 instanceof Document) {
  ((Document) node1).normalizeDocument();
  ((Document) node2).normalizeDocument();
 } else {
  node1.normalize();
  node2.normalize();
 }
 return nodeToString(node1).equals(nodeToString(node2));
}

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

private Node findSqlFragment(String refid, Properties variables) {
 refid = PropertyParser.parse(refid, variables);
 refid = builderAssistant.applyCurrentNamespace(refid, true);
 try {
  XNode nodeToInclude = configuration.getSqlFragments().get(refid);
  return nodeToInclude.getNode().cloneNode(true);
 } catch (IllegalArgumentException e) {
  throw new IncompleteElementException("Could not find SQL statement to include with refid '" + refid + "'", e);
 }
}

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

private Node findSqlFragment(String refid, Properties variables) {
 refid = PropertyParser.parse(refid, variables);
 refid = builderAssistant.applyCurrentNamespace(refid, true);
 try {
  XNode nodeToInclude = configuration.getSqlFragments().get(refid);
  return nodeToInclude.getNode().cloneNode(true);
 } catch (IllegalArgumentException e) {
  throw new IncompleteElementException("Could not find SQL statement to include with refid '" + refid + "'", e);
 }
}

代码示例来源:origin: org.apache.xmlbeans/xmlbeans

elem.setAttributeNodeNS( (Attr) attrs.item( i ).cloneNode( true ) );

相关文章