当前位置: 首页>>代码示例>>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;未经允许,请勿转载。