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.Node;

public class Main {
    public static void setNamedChildValue(Element element, String name, String value) throws Exception {
        Element e = getNamedChild(element, name);
        if (e == null)
            throw new Exception("unable to find element " + name);
        e.setAttribute("value", value);
    }

    public static Element getNamedChild(Element e, String name) {
        Element c = getFirstChild(e);
        while (c != null && !name.equals(c.getLocalName()) && !name.equals(c.getNodeName()))
            c = getNextSibling(c);
        return c;
    }

    public static Element getFirstChild(Element e) {
        if (e == null)
            return null;
        Node n = e.getFirstChild();
        while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
            n = n.getNextSibling();
        return (Element) n;
    }

    public static Element getNextSibling(Element e) {
        Node n = e.getNextSibling();
        while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
            n = n.getNextSibling();
        return (Element) n;
    }
}