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

/**
 * Return the root node of a XML document.
 * @param fileNameXML name of the XML file
 * @throws ParserConfigurationException//from  w  w w.  ja v  a  2s .  com
 * @throws SAXException
 * @throws IOException
 */
public static Document getXMLDocument(String fileNameXML)
        throws ParserConfigurationException, SAXException, IOException {
    File file = new File(fileNameXML);
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    doc.getDocumentElement().normalize();
    return doc;
}

From source file:Main.java

public static Document read(String path) {
    Document doc = null;//from w  w  w  .  j a  va2s  .  c om
    try {
        File file = new File(path);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        if (file.length() != 0) {
            doc = db.parse(file);
        } else {
            return null;
        }
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return doc;
}

From source file:Main.java

public static Document parse(String xml) {
    try {//from w  ww.ja v  a 2 s  .c  om
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        Document document = db.parse(stream);
        return document;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getCharttype() {
    try {// w ww .  jav  a 2  s .  com
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName("chartType");
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();
        return chartType;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Document parseXmlString(String xmlString) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    return docBuilder.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
}

From source file:Main.java

public static Document CreateCustomerFile(JFrame mainFrame) {
    Document CustomerDoc = null;//  ww  w.  j  a va  2s.c  o  m
    try {
        DocumentBuilderFactory Factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder Build = Factory.newDocumentBuilder();
        CustomerDoc = Build.newDocument();
        Element rootElem = CustomerDoc.createElement("Customers");
        CustomerDoc.appendChild(rootElem);
    } catch (ParserConfigurationException ex) {
        System.out.println(ex.getMessage());
        JOptionPane.showMessageDialog(mainFrame,
                "There Was an Error Saving The File. Please Restart the Application.");
        System.exit(1);
    }
    return CustomerDoc;
}

From source file:Main.java

/**
 * Parse a XML file and return the resulting DOM tree.
 *///  ww  w .  ja  va 2s .c  om
public static Document readXml(URL xmlUrl) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = newNamespaceAwareFactory();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputStream input = xmlUrl.openStream();
    try {
        return builder.parse(input);
    } finally {
        input.close();
    }
}

From source file:Main.java

public static Document getDocumentFromString(String xml) {
    Document retVal = null;//ww w .  ja v  a 2s.  c  o  m
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        retVal = db.parse(getInputSourceFromString(xml));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return retVal;
}

From source file:Main.java

static DocumentBuilder getDocumentBuilder() {
    DocumentBuilder documentBuilder = null;
    try {//w  ww.  jav a2  s . co  m
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilder = documentBuilderFactory.newDocumentBuilder();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return documentBuilder;
}

From source file:Main.java

public static Document createNewXMLDocument() throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();

    return document;
}