Here you can find the source of copyElement(Element orig, Document newOwner, Set
orig
, considering only types Element
and Attr
.
Parameter | Description |
---|---|
orig | <code>Element</code> to copy recursively |
newOwner | <code>Document</code> for which returned <code>Element</code> should be owned by |
blankTextNodes | <code>Set</code> of tag names that should not have any text. This is necessary since the method <code>Element.getTextValue()</code> returns the text content of this node and <i> all of its decendents</i> |
Element
tree that is copy of orig
but owned by newOwner
public static Element copyElement(Element orig, Document newOwner, Set<String> blankTextNodes)
//package com.java2s; /*/*from www. j av a 2 s.c o m*/ * Copyright (c) 2013 Game Salutes. * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v3 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/lgpl.html * * Contributors: * Game Salutes - Repackaging and modifications of original work under University of Chicago and Apache License 2.0 shown below * * Repackaging from edu.uchicago.nsit.iteco.utils to com.gamesalutes.utils * * Copyright 2008 - 2011 University of Chicago * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.util.*; import org.w3c.dom.*; public class Main { /** * Makes a copy of the tree rooted at <code>orig</code>, considering * only types <code>Element</code> and <code>Attr</code>. Comments and * other node types are ignored. * * @param orig <code>Element</code> to copy recursively * @param newOwner <code>Document</code> for which returned <code>Element</code> * should be owned by * @param blankTextNodes <code>Set</code> of tag names that should not have any text. * This is necessary since the method <code>Element.getTextValue()</code> * returns the text content of this node and <i> all of its * decendents</i> * @return new <code>Element</code> tree that is copy of <code>orig</code> but * owned by <code>newOwner</code> */ public static Element copyElement(Element orig, Document newOwner, Set<String> blankTextNodes) { String tagName = orig.getTagName(); Element e = newOwner.createElement(tagName); //copy attributes NamedNodeMap atts = orig.getAttributes(); for (int i = 0, len = atts.getLength(); i < len; ++i) { Attr origAttr = (Attr) atts.item(i); Attr newAttr = newOwner.createAttribute(origAttr.getName()); newAttr.setValue(origAttr.getValue()); e.setAttributeNode(newAttr); } //copy the text if not a node that shouldn't have text //this has to be done since getTextContent returns text content //of this node AND its decendants if (!blankTextNodes.contains(tagName)) e.setTextContent(orig.getTextContent()); //copy children recursively NodeList children = orig.getChildNodes(); for (int i = 0, len = children.getLength(); i < len; ++i) { Node n = children.item(i); if (n instanceof Element) e.appendChild(copyElement((Element) n, newOwner, blankTextNodes)); } return e; } }