copy a single XML attribute (if exist) - Java XML

Java examples for XML:DOM Node

Description

copy a single XML attribute (if exist)

Demo Code

/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are 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
 *
 * Contributors:/*from  w  w w . j a  v  a  2s  .  c om*/
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
//package com.java2s;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;

public class Main {
    /**
     * copy a single attribute (if exist)
     * 
     * @param source
     * @param srcattr
     * @param dest
     * @param destattr
     */
    public static void copyAttribute(Element source, String srcattr,
            Element dest, String destattr) {
        Attr attr = source.getAttributeNode(srcattr);
        if (attr != null) {
            dest.setAttribute(destattr, attr.getValue());
        }
    }
}

Related Tutorials