Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static Node findNamedElementNode(Node doc, String name) {
        Node[] elnodes = getChildNodes(doc, Node.ELEMENT_NODE);
        for (int i = 0; i < elnodes.length; ++i) {
            if (elnodes[i].getNodeName().equals(name)) {
                return elnodes[i];
            }
        }
        return null;
    }

    public static Node[] getChildNodes(Node node, short type) {
        NodeList children = node.getChildNodes();
        if (children == null) {
            return new Node[0];
        }
        int n = children.getLength();
        List<Node> elnodelist = new ArrayList<Node>(n);
        for (int i = 0; i < n; ++i) {
            Node childnode = children.item(i);
            if (childnode.getNodeType() == type) {
                elnodelist.add(childnode);
            }
        }
        Node[] empty = {};
        return elnodelist.toArray(empty);
    }
}