Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayInputStream;

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

import org.w3c.dom.Document;

public class Main {
    private static DocumentBuilderFactory mFactory = null;

    /**
     * @param xml is XML for a document
     * @return the Document contained in that XML
     */
    public static Document readString(String xml) {
        ensureFactory();
        try {
            DocumentBuilder builder = mFactory.newDocumentBuilder();
            return builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static synchronized void ensureFactory() {
        if (mFactory == null) {
            mFactory = DocumentBuilderFactory.newInstance();
            mFactory.setValidating(false);
            mFactory.setCoalescing(true);
            mFactory.setExpandEntityReferences(false);
        }
    }
}