Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {

    public static void overrideXmlList(NodeList target, String targetTag, NodeList parent, String parentTag) {

        for (int i = 0; i < target.getLength(); i++) {
            Element targetColumn = (Element) target.item(i);
            Element parentColumn = null;

            String targetTagValue = targetColumn.getAttribute(targetTag);
            for (int j = 0; j < parent.getLength(); j++) {

                if (targetTagValue.equals(((Element) parent.item(j)).getAttribute(parentTag))) {
                    parentColumn = (Element) parent.item(j);
                    break;
                }
            }

            if (parentColumn != null) {
                overrideXml(targetColumn, parentColumn);
            }
        }
    }

    public static Element overrideXml(Element target, Element parent) {

        if (parent != null) {

            NamedNodeMap namedNodeMap = parent.getAttributes();
            for (int i = 0; i < namedNodeMap.getLength(); i++) {

                Node attributeNode = namedNodeMap.item(i);
                String parentAttributeName = attributeNode.getNodeName();
                String parentAttributeValue = attributeNode.getNodeValue();

                // attribute override
                if (!target.hasAttribute(parentAttributeName)) {
                    target.setAttribute(parentAttributeName, parentAttributeValue);
                }

                // children override
                if (parent.getChildNodes().getLength() > 0) {
                    if (target.getChildNodes().getLength() == 0) {
                        for (int j = 0; j < target.getChildNodes().getLength(); j++) {

                            target.appendChild(target.getChildNodes().item(j));
                        }
                    }
                }

            }
        }

        return target;
    }
}