當前位置: 首頁>>代碼示例>>Java>>正文


Java Node.cloneNode方法代碼示例

本文整理匯總了Java中org.w3c.dom.Node.cloneNode方法的典型用法代碼示例。如果您正苦於以下問題:Java Node.cloneNode方法的具體用法?Java Node.cloneNode怎麽用?Java Node.cloneNode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.w3c.dom.Node的用法示例。


在下文中一共展示了Node.cloneNode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: translateXML

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * 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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:31,代碼來源:XMLUtil.java

示例2: createCopy

import org.w3c.dom.Node; //導入方法依賴的package包/類
static protected void createCopy(Step step, Document doc, Element stepNode) throws EngineException {
	NodeList list = step.getContextValues();
	if (list != null) {
		int len = list.getLength();
		for (int i=0; i<len;i++) {
			Node node = list.item(i);
			if (node != null) {
				boolean shouldImport = !node.getOwnerDocument().equals(doc);
				Node child = shouldImport ? doc.importNode(node, true):node.cloneNode(true);
				if (child.getNodeType() == Node.ELEMENT_NODE) {
					stepNode.appendChild((Element) child);
				} else if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
					stepNode.setAttribute(child.getNodeName(),child.getNodeValue());
				}
			}
		}
	}
}
 
開發者ID:convertigo,項目名稱:convertigo-engine,代碼行數:19,代碼來源:XMLCopyStep.java

示例3: decode

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Decodes the given XML node. The optional "into" argument specifies an existing object to be
 * used. If no object is given, then a new instance is created using the constructor from the
 * codec.
 * 
 * The function returns the passed in object or the new instance if no object was given.
 * 
 * @param node XML node to be decoded.
 * @param into Optional object to be decodec into.
 * @return Returns an object that represents the given node.
 */
public Object decode(Node node, Object into) {
  Object obj = null;

  if (node != null && node.getNodeType() == Node.ELEMENT_NODE) {
    mxObjectCodec codec = mxCodecRegistry.getCodec(node.getNodeName());

    try {
      if (codec != null) {
        obj = codec.decode(this, node, into);
      } else {
        obj = node.cloneNode(true);
        ((Element) obj).removeAttribute("as");
      }
    } catch (Exception e) {
      System.err.println("Cannot decode " + node.getNodeName() + ": " + e.getMessage());
      e.printStackTrace();
    }
  }

  return obj;
}
 
開發者ID:ModelWriter,項目名稱:Tarski,代碼行數:33,代碼來源:mxCodec.java

示例4: testImportNode

import org.w3c.dom.Node; //導入方法依賴的package包/類
@Test
public void testImportNode() throws Exception {
    Document document = createDOMWithNS("Node02.xml");
    Document otherDocument = createDOMWithNS("ElementSample01.xml");

    NodeList otherNodeList = otherDocument.getElementsByTagName("body");
    Node importedNode = otherNodeList.item(0);
    Node clone = importedNode.cloneNode(true);

    Node retNode = document.importNode(importedNode, true);
    assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed
    assertNotEquals(retNode, importedNode);
    assertTrue(importedNode.isEqualNode(retNode));

    retNode = document.importNode(importedNode, false);
    assertTrue(clone.isEqualNode(importedNode)); //verify importedNode is not changed
    assertEquals(retNode.getNodeName(), importedNode.getNodeName());
    assertFalse(importedNode.isEqualNode(retNode));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:NodeTest.java

示例5: copyDocument

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Copy elements from one document to another attaching at the specified element
 * and translating the namespace.
 *
 * @param from copy the children of this element (exclusive)
 * @param to where to attach the copied elements
 * @param newNamespace destination namespace
 * 
 * @since 8.4
 */
public static void copyDocument(Element from, Element to, String newNamespace) {
    Document doc = to.getOwnerDocument();
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode = null;
        if (Node.ELEMENT_NODE == node.getNodeType()) {
            Element oldElement = (Element) node;
            newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
            NamedNodeMap m = oldElement.getAttributes();
            Element newElement = (Element) newNode;
            for (int index = 0; index < m.getLength(); index++) {
                Node attr = m.item(index);
                newElement.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            copyDocument(oldElement, newElement, newNamespace);
        } else {
            newNode = node.cloneNode(true);
            newNode = to.getOwnerDocument().importNode(newNode, true);
        }
        if (newNode != null) {
            to.appendChild(newNode);
        }
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:37,代碼來源:XMLUtil.java

示例6: convertXML

import org.w3c.dom.Node; //導入方法依賴的package包/類
@Override
protected DocumentFragment convertXML(Element xmlValue) throws ConverterException {
	Document doc = xmlValue.getOwnerDocument();
	DocumentFragment result = doc.createDocumentFragment();
	for (Node child : XMLUtils.childrenNodes(xmlValue)) {
		Node clone = child.cloneNode(true);
		result.appendChild(clone);
	}
	return result;
}
 
開發者ID:Bibliome,項目名稱:alvisnlp,代碼行數:11,代碼來源:DOMParamConverter.java

示例7: cloneNodeWithUserData

import org.w3c.dom.Node; //導入方法依賴的package包/類
private static Node cloneNodeWithUserData(Node node, boolean recurse) {
	if (node != null) {
		Object node_output = node.getUserData(Step.NODE_USERDATA_OUTPUT);

		Node clonedNode = node.cloneNode(false);
		clonedNode.setUserData(Step.NODE_USERDATA_OUTPUT, node_output, null);
		
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			// attributes
			NamedNodeMap attributeMap = clonedNode.getAttributes();
			for (int i=0; i< attributeMap.getLength(); i++) {
				Node clonedAttribute = attributeMap.item(i);
				String attr_name = clonedAttribute.getNodeName();
				Object attr_output = ((Element)node).getAttributeNode(attr_name).getUserData(Step.NODE_USERDATA_OUTPUT);
				clonedAttribute.setUserData(Step.NODE_USERDATA_OUTPUT, attr_output, null);
			}
			
			// recurse on element child nodes only
			if (recurse && node.hasChildNodes()) {
				NodeList list = node.getChildNodes();
				for (int i=0; i<list.getLength(); i++) {
					Node clonedChild = cloneNodeWithUserData(list.item(i), recurse);
					if (clonedChild != null) {
						clonedNode.appendChild(clonedChild);
					}
				}
			}
		}
		
		return clonedNode;
	}
	return null;
}
 
開發者ID:convertigo,項目名稱:convertigo-engine,代碼行數:34,代碼來源:Sequence.java

示例8: decode

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Decodes the given XML node. The optional "into" argument specifies an
 * existing object to be used. If no object is given, then a new
 * instance is created using the constructor from the codec.
 * 
 * The function returns the passed in object or the new instance if no
 * object was given.
 * 
 * @param node XML node to be decoded.
 * @param into Optional object to be decodec into.
 * @return Returns an object that represents the given node.
 */
public Object decode(Node node, Object into)
{
	Object obj = null;

	if (node != null && node.getNodeType() == Node.ELEMENT_NODE)
	{
		mxObjectCodec codec = mxCodecRegistry.getCodec(node.getNodeName());

		try
		{
			if (codec != null)
			{
				obj = codec.decode(this, node, into);
			}
			else
			{
				obj = node.cloneNode(true);
				((Element) obj).removeAttribute("as");
			}
		}
		catch (Exception e)
		{
			System.err.println("Cannot decode " + node.getNodeName() + ": "
					+ e.getMessage());
			e.printStackTrace();
		}
	}

	return obj;
}
 
開發者ID:GDSRS,項目名稱:TrabalhoFinalEDA2,代碼行數:43,代碼來源:mxCodec.java

示例9: beforeDecode

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Reads the cells into the graph model. All cells are children of the root
 * element in the node.
 */
public Node beforeDecode(mxCodec dec, Node node, Object into)
{
	if (into instanceof mxRootChange)
	{
		mxRootChange change = (mxRootChange) into;

		if (node.getFirstChild() != null
				&& node.getFirstChild().getNodeType() == Node.ELEMENT_NODE)
		{
			// Makes sure the original node isn't modified
			node = node.cloneNode(true);

			Node tmp = node.getFirstChild();
			change.setRoot(dec.decodeCell(tmp, false));

			Node tmp2 = tmp.getNextSibling();
			tmp.getParentNode().removeChild(tmp);
			tmp = tmp2;

			while (tmp != null)
			{
				tmp2 = tmp.getNextSibling();

				if (tmp.getNodeType() == Node.ELEMENT_NODE)
				{
					dec.decodeCell(tmp, true);
				}

				tmp.getParentNode().removeChild(tmp);
				tmp = tmp2;
			}
		}
	}

	return node;
}
 
開發者ID:GDSRS,項目名稱:TrabalhoFinalEDA2,代碼行數:41,代碼來源:mxRootChangeCodec.java

示例10: beforeDecode

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Reads the cells into the graph model. All cells are children of the root element in the node.
 */
public Node beforeDecode(mxCodec dec, Node node, Object into) {
  if (into instanceof mxRootChange) {
    mxRootChange change = (mxRootChange) into;

    if (node.getFirstChild() != null && node.getFirstChild().getNodeType() == Node.ELEMENT_NODE) {
      // Makes sure the original node isn't modified
      node = node.cloneNode(true);

      Node tmp = node.getFirstChild();
      change.setRoot(dec.decodeCell(tmp, false));

      Node tmp2 = tmp.getNextSibling();
      tmp.getParentNode().removeChild(tmp);
      tmp = tmp2;

      while (tmp != null) {
        tmp2 = tmp.getNextSibling();

        if (tmp.getNodeType() == Node.ELEMENT_NODE) {
          dec.decodeCell(tmp, true);
        }

        tmp.getParentNode().removeChild(tmp);
        tmp = tmp2;
      }
    }
  }

  return node;
}
 
開發者ID:ModelWriter,項目名稱:Tarski,代碼行數:34,代碼來源:mxRootChangeCodec.java

示例11: testCloneNode

import org.w3c.dom.Node; //導入方法依賴的package包/類
@Test
public void testCloneNode() throws Exception {
    Document document = createDOMWithNS("Node02.xml");

    NodeList nodeList = document.getElementsByTagName("body");
    Node node = nodeList.item(0);
    Node cloneNode = node.cloneNode(true);

    assertTrue(node.isEqualNode(cloneNode));
    assertNotEquals(node, cloneNode);

    nodeList = document.getElementsByTagName("html");
    Node node2 = nodeList.item(0);
    node2.appendChild(cloneNode);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:NodeTest.java

示例12: remove

import org.w3c.dom.Node; //導入方法依賴的package包/類
private final Node remove(AttrImpl attr, int index,
                          boolean addDefault) {

    CoreDocumentImpl ownerDocument = ownerNode.ownerDocument();
    String name = attr.getNodeName();
    if (attr.isIdAttribute()) {
        ownerDocument.removeIdentifier(attr.getValue());
    }

    if (hasDefaults() && addDefault) {
        // If there's a default, add it instead
        NamedNodeMapImpl defaults =
            ((ElementImpl) ownerNode).getDefaultAttributes();

        Node d;
        if (defaults != null &&
            (d = defaults.getNamedItem(name)) != null &&
            findNamePoint(name, index+1) < 0) {
                NodeImpl clone = (NodeImpl)d.cloneNode(true);
                if (d.getLocalName() !=null){
                        // we must rely on the name to find a default attribute
                        // ("test:attr"), but while copying it from the DOCTYPE
                        // we should not loose namespace URI that was assigned
                        // to the attribute in the instance document.
                        ((AttrNSImpl)clone).namespaceURI = attr.getNamespaceURI();
                }
                clone.ownerNode = ownerNode;
                clone.isOwned(true);
                clone.isSpecified(false);

                nodes.set(index, clone);
                if (attr.isIdAttribute()) {
                    ownerDocument.putIdentifier(clone.getNodeValue(),
                                            (ElementImpl)ownerNode);
                }
        } else {
            nodes.remove(index);
        }
    } else {
        nodes.remove(index);
    }

    //        changed(true);

    // remove reference to owner
    attr.ownerNode = ownerDocument;
    attr.isOwned(false);

    // make sure it won't be mistaken with defaults in case it's
    // reused
    attr.isSpecified(true);
    attr.isIdAttribute(false);

    // notify document
    ownerDocument.removedAttrNode(attr, ownerNode, name);

    return attr;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:59,代碼來源:AttributeMap.java

示例13: traverseFullySelected

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Utility method for traversing a single node when
 * we know a-priori that the node if fully
 * selected.
 *
 * @param n      The node to be traversed.
 *
 * @param how    Specifies what type of traversal is being
 *               requested (extract, clone, or delete).
 *               Legal values for this argument are:
 *
 *               <ol>
 *               <li><code>EXTRACT_CONTENTS</code> - will simply
 *               return the original node.
 *
 *               <li><code>CLONE_CONTENTS</code> - will leave the
 *               context tree of the range undisturbed, but will
 *               return a cloned node.
 *
 *               <li><code>DELETE_CONTENTS</code> - will delete the
 *               node from it's parent, but will return null.
 *               </ol>
 *
 * @return Returns a node that is the result of visiting the node.
 *         If the traversal operation is
 *         <code>DELETE_CONTENTS</code> the return value is null.
 */
private Node traverseFullySelected( Node n, int how )
{
    switch( how )
    {
    case CLONE_CONTENTS:
        return n.cloneNode( true );
    case EXTRACT_CONTENTS:
        if ( n.getNodeType()==Node.DOCUMENT_TYPE_NODE )
        {
            // TBD: This should be a HIERARCHY_REQUEST_ERR
            throw new DOMException(
                    DOMException.HIERARCHY_REQUEST_ERR,
            DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "HIERARCHY_REQUEST_ERR", null));
        }
        return n;
    case DELETE_CONTENTS:
        n.getParentNode().removeChild(n);
        return null;
    }
    return null;
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:49,代碼來源:RangeImpl.java

示例14: processXSParticle

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Processes an XSParticle.
 * 
 * @param particle the particle to process.
 * @param contentElem the DOM element to be populated.
 */
protected void processXSParticle(XSParticle particle,
                                 Element contentElem) throws Exception {
   if (null != particle && null != contentElem) {
      XSTerm term = particle.getTerm();
      if (term instanceof XSModelGroup) {
         processXSGroup((XSModelGroup) term, contentElem);
      }
      else if (term instanceof XSElementDeclaration) {
         String termName = term.getName();
         int numOccurs = getOccurances(particle, contentElem);
         if ((0 < numOccurs) || sampleXML) {
            Node currentNode = parseXSObject((XSElementDeclaration) term, contentElem);
            for (int i = 0; i < numOccurs - 1; i++) {
               Node newElem = currentNode.cloneNode(true);
               if (!(currentNode.getParentNode() instanceof Document)) {
                  currentNode.getParentNode().appendChild(newElem);
               }
               NamedNodeMap attributes = newElem.getAttributes();
               for (int j = 0; j < attributes.getLength(); j++) {
                  String attributeValue = getAttributeValue(attributes.item(j).getNodeName(), newElem);
                  if (attributeValue != null) {
                     attributes.item(j).setNodeValue(attributeValue);
                  }
               }
               assignNodeValue(newElem);
            }
         }

      }
      else if (term instanceof XSWildcard) {
         if (soapEnv && xsRootElement.getName().equals("Envelope")
             && getNodeName(contentElem).equals("Body")) {
            // soapEnv == true, and we have traversed down to the 'Body' element.
            // Now change xsRootSchemaElement and build payload.
            XSElementDeclaration newRoot = stripSoapEnv();
            if (null != newRoot){
         	   parseXSObject(newRoot, contentElem);
            }
         }
      }
      else {
         log.warn("Unprocessed term case:" + ((null == term) ? "" : term.getClass().toString()));
      }
   }
}
 
開發者ID:mqsysadmin,項目名稱:dpdirect,代碼行數:52,代碼來源:SchemaLoader.java

示例15: beforeDecode

import org.w3c.dom.Node; //導入方法依賴的package包/類
/**
 * Reads the cells into the graph model. All cells are children of the root
 * element in the node.
 */
public Node beforeDecode(mxCodec dec, Node node, Object into)
{
	if (into instanceof mxChildChange)
	{
		mxChildChange change = (mxChildChange) into;

		if (node.getFirstChild() != null
				&& node.getFirstChild().getNodeType() == Node.ELEMENT_NODE)
		{
			// Makes sure the original node isn't modified
			node = node.cloneNode(true);

			Node tmp = node.getFirstChild();
			change.setChild(dec.decodeCell(tmp, false));

			Node tmp2 = tmp.getNextSibling();
			tmp.getParentNode().removeChild(tmp);
			tmp = tmp2;

			while (tmp != null)
			{
				tmp2 = tmp.getNextSibling();

				if (tmp.getNodeType() == Node.ELEMENT_NODE)
				{
					// Ignores all existing cells because those do not need
					// to be re-inserted into the model. Since the encoded
					// version of these cells contains the new parent, this
					// would leave to an inconsistent state on the model
					// (ie. a parent change without a call to
					// parentForCellChanged).
					String id = ((Element) tmp).getAttribute("id");

					if (dec.lookup(id) == null)
					{
						dec.decodeCell(tmp, true);
					}
				}

				tmp.getParentNode().removeChild(tmp);
				tmp = tmp2;
			}
		}
		else
		{
			String childRef = ((Element) node).getAttribute("child");
			change.setChild((mxICell) dec.getObject(childRef));
		}
	}

	return node;
}
 
開發者ID:GDSRS,項目名稱:TrabalhoFinalEDA2,代碼行數:57,代碼來源:mxChildChangeCodec.java


注:本文中的org.w3c.dom.Node.cloneNode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。