class XMLUtilities {

    public static void mergeDuplicateElements(Document doc, String tagName)
    {
        NodeList elements = doc.getElementsByTagName(tagName);
        if (elements.getLength() > 1)
        {
            Node toNode = elements.item(0);
            for (int index = elements.getLength() - 1; index >= 1; --index)
            {
                Node fromNode = elements.item(index);
                while (fromNode.hasChildNodes())
                {
                    toNode.appendChild(fromNode.removeChild(fromNode.getFirstChild()));
                }
                fromNode.getParentNode().removeChild(fromNode);
            }
        }
    }

    private  XMLUtilities();

}