Changeset 3985


Ignore:
Timestamp:
2003-03-25T16:11:19+12:00 (21 years ago)
Author:
mdewsnip
Message:

Added function for returning a copy of an element with a different name.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl3/src/java/org/greenstone/gsdl3/util/GSXML.java

    r3969 r3985  
    11package org.greenstone.gsdl3.util;
    22
     3import org.w3c.dom.NamedNodeMap;
    34import org.w3c.dom.Node;
    45import org.w3c.dom.Element;
     
    316317       
    317318
     319    /** Duplicates an element, but gives it a new name */
     320    public static Element duplicateWithNewName(Document owner, Element element,
     321                           String element_name, boolean with_attributes)
     322    {
     323    Element duplicate = owner.createElement(element_name);
     324
     325    // Copy element attributes
     326    if (with_attributes) {
     327        NamedNodeMap attributes = element.getAttributes();
     328        for (int i = 0; i < attributes.getLength(); i++) {
     329        Node attribute = attributes.item(i);
     330        duplicate.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
     331        }
     332    }
     333
     334    // Copy element children
     335    NodeList children = element.getChildNodes();
     336    for (int i = 0; i < children.getLength(); i++) {
     337        Node child = children.item(i);
     338        duplicate.appendChild(owner.importNode(child, true));
     339    }
     340
     341    return duplicate;
     342    }
     343
     344
    318345    /** returns a basic request message */
    319346    public static  Element createBasicRequest(Document owner,
Note: See TracChangeset for help on using the changeset viewer.