本文整理匯總了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;
}
示例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());
}
}
}
}
}
示例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;
}
示例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));
}
示例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);
}
}
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}
示例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;
}
示例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()));
}
}
}
示例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;
}