Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: GNU General Public License 

import java.io.IOException;

import java.io.Reader;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

public class Main {
    /**
     * Convert String to an XML document.
     */
    public static Document stringToXML(String xml) {
        try {
            return parseReader(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Document parseReader(Reader reader)
            throws org.xml.sax.SAXException, IOException, ParserConfigurationException {
        return newDocumentBuilder().parse(new org.xml.sax.InputSource(reader));
    }

    private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
        // SAXParsers are not concurrency compatible, so always return a new instance to prevent thread issues 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        return dbf.newDocumentBuilder();
    }
}