Java XML Attribute Copy copyAttributes(Node sourceNode, Element visualElement)

Here you can find the source of copyAttributes(Node sourceNode, Element visualElement)

Description

Copies all attributes from source node to visual node.

License

Open Source License

Parameter

Parameter Description
sourceNode the source node
visualElement the visual element

Declaration

public static void copyAttributes(Node sourceNode, Element visualElement) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007-2014 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributor://from  w w w . ja va  2  s.  c  o  m
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class Main {
    /**
     * Copies all attributes from source node to visual node.
     * 
     * @param sourceNode the source node
     * @param visualElement the visual element
     */
    public static void copyAttributes(Node sourceNode, Element visualElement) {
        NamedNodeMap namedNodeMap = sourceNode.getAttributes();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {
            Node attribute = namedNodeMap.item(i);
            // added by Max Areshkau fix for JBIDE-1568
            visualElement.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
        }
    }

    /**
     * Copies all attributes from source node to visual node.
     * 
     * @param sourceElement the source element
     * @param visualElement the visual element
     * @param attributes list names of attributes which will copy
     */
    public static void copyAttributes(Element sourceElement, Element visualElement, List<String> attributes) {
        for (String attributeName : attributes) {
            copyAttribute(sourceElement, attributeName, visualElement, attributeName);
        }
    }

    /**
     * Copies all attributes from source node to visual node.
     * 
     * @param sourceElement the source element
     * @param visualElement the visual element
     * @param sourceToVisualMap mapping for attributes' names.
     */
    public static void copyAttributes(Element sourceElement, Element visualElement,
            Map<String, String> sourceToVisualMap) {
        for (Entry<String, String> sourceToVisual : sourceToVisualMap.entrySet()) {
            String sourceAttrName = sourceToVisual.getKey();
            String visualAttrName = sourceToVisual.getValue();
            copyAttribute(sourceElement, sourceAttrName, visualElement, visualAttrName);
        }
    }

    /**
     * Copies all attributes from source node to visual node.
     * 
     * @param sourceElement the source element
     * @param sourceAttrName the name of source attribute 
     * @param visualElement the visual element
     * @param visualAttrName the resulting name of visual attribute 
     */
    public static void copyAttribute(Element sourceElement, String sourceAttrName, Element visualElement,
            String visualAttrName) {
        if (sourceElement.hasAttribute(sourceAttrName)) {
            String attrValue = sourceElement.getAttribute(sourceAttrName);
            visualElement.setAttribute(visualAttrName, attrValue);
        }
    }
}

Related

  1. copyAttributes(Element from, Element to)
  2. copyAttributes(Element from, Element to, NodeFilter filter)
  3. copyAttributes(Element fromEl, Element toEl)
  4. copyAttributes(final Element destElement, final Element srcElement)
  5. copyAttributes(Node from, Node to)
  6. copyAttributes(SOAPElement target, Element source)