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.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;
import org.w3c.dom.Element;

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

public class Main {
    /**
     * Transforms a string representation to a DOM representation
     * @param xmlString XML as string
     * @return DOM representation of String
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Element stringToDOM(String xmlString)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);

        DocumentBuilder builder = dbf.newDocumentBuilder();

        Reader reader = new StringReader(xmlString);
        InputSource src = new InputSource(reader);
        Document domDoc = builder.parse(src);
        return domDoc.getDocumentElement();
    }
}