Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import org.w3c.dom.*;

import java.util.*;

public class Main {
    public static Map<String, String> getChildTextMap(Element elt) {
        Map<String, String> data = new HashMap<String, String>();
        NodeList nodes = elt.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) child;
                data.put(e.getNodeName(), getInnerText(e));
            }
        }
        return data;
    }

    public static void getInnerText(StringBuilder sb, Element elt, String separator) {
        NodeList tns = elt.getChildNodes();
        for (int j = 0; j < tns.getLength(); j++) {
            Node tn = tns.item(j);
            if (tn.getNodeType() == Node.TEXT_NODE) {
                sb.append(tn.getNodeValue());
            } else if (tn.getNodeType() == Node.ELEMENT_NODE) {
                sb.append(separator);
                getInnerText(sb, (Element) tn, separator);
                sb.append(separator);
            }
        }
    }

    public static String getInnerText(Element elt) {

        StringBuilder sb = new StringBuilder();
        getInnerText(sb, elt, " ");
        return sb.toString();
    }
}