Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// License as published by the Free Software Foundation; either

import org.w3c.dom.Element;

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

public class Main {
    public static Element getChildWithAttributeValue(Element element, String attributeName, String attributeValue) {

        if (element == null) {
            return null;
        }

        NodeList children;

        try {
            children = element.getChildNodes();
        } catch (IllegalArgumentException e) {
            // We've seen this throw a IllegalArgumentException from
            // com.sun.org.apache.xerces.internal.dom.DeferredDocumentImpl.getNodeObject(DeferredDocumentImpl.java:1081)
            // under GWT 1.7

            return null;
        }

        int length;

        try {
            length = children.getLength();
        } catch (NullPointerException e) {
            // We've seen this throw a NullPointerException from
            // com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListGetLength(ParentNode.java:696)
            // under GWT 1.7

            return null;
        }

        for (int loop = 0; loop < length; loop++) {
            Node node;

            try {
                node = children.item(loop);
            } catch (NullPointerException e) {
                // We've seen this throw a NullPointerException from
                // com.sun.org.apache.xerces.internal.dom.ParentNode.nodeListItem(ParentNode.java:780)
                // under GWT 1.7

                continue;
            }

            if (!(node instanceof Element)) {
                continue;
            }

            Element child = (Element) node;

            try {
                if (attributeValue.equals(child.getAttribute(attributeName))) {
                    return child;
                }
            } catch (NullPointerException e) {
                // We've seen this throw a NullPointerException from
                // com.sun.org.apache.xerces.internal.dom.DeferredAttrNSImpl.synchronizeData(DeferredAttrNSImpl.java:97)
                // under GWT 1.7

                continue;
            }
        }

        return null;
    }
}