Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;
import java.io.StringReader;

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

import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**
     * @param string Creates a {@link Document} from a string.
     * @return A {@link Document} representation of the string.
     * @throws ParserConfigurationException if a DocumentBuilder cannot be
     * created which satisfies the configuration requested
     * @throws SAXException if any parse errors occur
     * @throws IOException if any IO errors occur.
     *
     */
    public static Document stringToXml(String string)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(string));
        if (builder.isNamespaceAware()) {
            System.out.println("#######################3Is aware");
        } else {
            System.out.println("#######################Not aware");
        }
        return builder.parse(is);
    }
}