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 {
    public static String setChildText(Element elt, String name, String value) {
        Element child = getChildElement(elt, name);

        if (child == null) {
            child = elt.getOwnerDocument().createElement(name);
            elt.appendChild(child);
        }
        return setInnerText(child, value);

    }

    public static Element getChildElement(Element elt, String name) {
        NodeList nodes = elt.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE)
                if (name.equals(node.getNodeName()))
                    return (Element) node;
        }
        return null;
    }

    public static String setInnerText(Element node, String value) {
        StringBuilder sb = new StringBuilder();
        while (node.hasChildNodes()) {
            Node tn = node.getFirstChild();
            if (tn.getNodeType() == Node.TEXT_NODE) {
                sb.append(tn.getNodeValue());
            }
            node.removeChild(tn);
        }
        node.appendChild(node.getOwnerDocument().createTextNode(value));
        return sb.toString();
    }
}