Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.Text;

public class Main {
    public static Document createXMLResult(String rootName, Map<String, String> map) {

        try {
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder;
            docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();
            Element root = doc.createElement(rootName);
            doc.appendChild(root);
            for (String elementName : map.keySet()) {
                Element child = doc.createElement(elementName);
                Text text = doc.createTextNode(map.get(elementName));
                child.appendChild(text);
                root.appendChild(child);
            }

            return doc;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    public static Document createXMLResult(String rootName, Map<String, String> map, Document d) {

        try {

            Element r = d.createElement(rootName);
            d.appendChild(r);

            for (String elementName : map.keySet()) {
                Element eltName = d.createElement(elementName);
                eltName.appendChild(d.createTextNode(map.get(elementName)));
                r.appendChild(eltName);
            }

            d.normalizeDocument();

            return d;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }
}