Example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

List of usage examples for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory newDocumentBuilder.

Prototype


public abstract DocumentBuilder newDocumentBuilder() throws ParserConfigurationException;

Source Link

Document

Creates a new instance of a javax.xml.parsers.DocumentBuilder using the currently configured parameters.

Usage

From source file:Main.java

/**
 * Returns a {@link org.w3c.dom.Document} from an {@link java.io.InputStream}.
 * @param inputStream the stream from which getting the XML content
 * @return the XML document or null if /*from w  w w.ja  va 2s. c  o m*/
 * @throws IOException if there was an error while getting the XML from the stream
 * @throws SAXException if there was an error while parsing the XML document
 * @throws javax.xml.parsers.ParserConfigurationException
 */
public static Document getDocument(InputStream inputStream)
        throws IOException, SAXException, ParserConfigurationException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(inputStream);
}

From source file:Main.java

public static Element getroot(String siteName) {
    Element root = null;/*from   ww w  .j a  v a  2  s  . c  om*/
    try {
        File fXmlFile = new File(siteName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.parse(fXmlFile);
        root = document.getDocumentElement();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return root;

}

From source file:Main.java

/**
 * @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.
 *
 */// w  ww .  j a v a  2  s.c  o m
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);
}

From source file:Main.java

/**
 * Creates ElementNode for given tag with particular text as content.
 *///w  w w. java  2  s . c  o  m
public static Element createTextNode(String tag, String content) throws ParserConfigurationException {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    Element node = doc.createElement(tag);
    Text t = doc.createTextNode(content);
    node.appendChild(t);

    return node;
}

From source file:Main.java

public static Document createXmlDoc(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(is);
    return doc;/*  w ww .j a  va  2 s .  c  o m*/
}

From source file:Main.java

private static Document parseXml(String xmlPath) {
    Document doc = null;//from www . j a  v a 2 s. c om
    try {
        File trainSchedule = new File(xmlPath);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(trainSchedule);
        doc.getDocumentElement().normalize();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if (doc == null)
        throw new ParserException("cannot parse: \"" + xmlPath + "\"");

    return doc;
}

From source file:Main.java

/**
 * Create a new blank XML document./*from  ww w.jav a2  s .  c  om*/
 *
 * @return The new blank XML document.
 *
 * @throws IOException If there is an error creating the XML document.
 */
public static Document newDocument() throws IOException {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        return builder.newDocument();
    } catch (Exception e) {
        IOException thrown = new IOException(e.getMessage());
        throw thrown;
    }
}

From source file:Main.java

public static Object getBean() {
    try {//  w ww . j  a  v  a 2  s . c o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File("src/config.xml"));

        NodeList nl = doc.getElementsByTagName("className");
        Node node = nl.item(0).getFirstChild();
        String cName = node.getNodeValue();

        Class clazz = Class.forName(cName);
        Object obj = clazz.newInstance();
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Create a new XML document.//  w  w w  . ja v a 2  s  .  co  m
 * 
 * @return document
 * @throws ParserConfigurationException 
 */
public static Document newXml() throws ParserConfigurationException {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    return docBuilder.newDocument();
}

From source file:Main.java

/**
 * Obtains DOMImpementaton interface providing a number of methods for performing 
 * operations that are independent of any particular DOM instance. 
 *
 * @throw DOMException <code>NOT_SUPPORTED_ERR</code> if cannot get DOMImplementation
 * @throw FactoryConfigurationError Application developers should never need to directly catch errors of this type.          
 *
 * @return DOMImplementation implementation
 *//*from w w  w.  ja va  2  s  . c  om*/
private static DOMImplementation getDOMImplementation() throws DOMException { //can be made public

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    try {
        return factory.newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException ex) {
        throw new DOMException(DOMException.NOT_SUPPORTED_ERR,
                "Cannot create parser satisfying configuration parameters"); //NOI18N
    }
}