Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    /**
     * Returns an element parent node with "name" attribute based on a complex
     * key filter (parent.child.child)
     * @param xsdDoc
     * @param xsdKey
     * @return
     */
    protected static Element getXsdParentComplexKey(Document xsdDoc, String xsdKey) {
        Element tmpRes = null;
        Element element = null;

        String[] splitStr = xsdKey.split("[.]");

        String topParent = splitStr[0];
        String Parent = splitStr[1];
        String Node = splitStr[2];

        try {
            NodeList allNodes = xsdDoc.getElementsByTagName("*");
            for (int i = 0; i < allNodes.getLength(); i++) {
                element = (Element) allNodes.item(i);
                if (element.getAttribute("name").equals(Node)) {
                    element = (Element) element.getParentNode();
                    while (!((Element) element).hasAttribute("name")) {
                        element = (Element) element.getParentNode();
                    }
                    if (element.getAttribute("name").equals(Parent)) {
                        Element pElement = (Element) element.getParentNode();
                        while (!((Element) pElement).hasAttribute("name")) {
                            pElement = (Element) pElement.getParentNode();
                        }
                        if (pElement.getAttribute("name").equals(topParent)) {
                            tmpRes = element;
                        }
                    }
                }
            }
            return tmpRes;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}