Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.Document;

import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;

import org.w3c.dom.ls.LSParser;

public class Main {
    private static DOMImplementationRegistry domImpRegistry = null;
    private static DOMImplementationLS domImpLS = null;

    /**
     * This method converts xml string to DOM Document
     *
     * @param xmlString
     *            XML string @return DOM Document @throws Exception @throws
     */
    public static Document loadXmlFromString(String xmlString) throws Exception {
        LSParser builder = getDOMImplementationLS().createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
        LSInput input = getDOMImplementationLS().createLSInput();
        input.setStringData(xmlString);
        return builder.parse(input);
    }

    /**
     * @return DOM Implementation Load Save
     * @throws Exception
     */
    private static DOMImplementationLS getDOMImplementationLS() throws Exception {
        if (domImpLS == null) {
            domImpLS = (DOMImplementationLS) getDOMImplementationRegistry().getDOMImplementation("LS");
        }
        return domImpLS;
    }

    /**
     * @return DOM Implementation Registry
     * @throws Exception
     */
    private static DOMImplementationRegistry getDOMImplementationRegistry() throws Exception {
        if (domImpRegistry == null) {
            domImpRegistry = DOMImplementationRegistry.newInstance();
        }
        return domImpRegistry;
    }
}