Java XML Document Create createDocument(String xml)

Here you can find the source of createDocument(String xml)

Description

convenience method to create an xml Document.

License

Apache License

Parameter

Parameter Description
xml the xml to parse

Exception

Parameter Description
ParserConfigurationException the parser configuration exception
SAXException the sAX exception
IOException Signals that an I/O exception has occurred.

Return

Document representing the xml

Declaration

public static Document createDocument(String xml)
        throws ParserConfigurationException, SAXException, IOException 

Method Source Code


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

import java.io.BufferedInputStream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

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

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    /**//from   w ww. ja  v  a2 s  . co  m
     * convenience method to create an xml Document.
     * 
     * @param xml
     *            the xml to parse
     * @return Document representing the xml
     * @throws ParserConfigurationException
     *             the parser configuration exception
     * @throws SAXException
     *             the sAX exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static Document createDocument(String xml)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()));
        doc.getDocumentElement().normalize();

        return doc;
    }

    /**
     * Creates the document.
     * 
     * @param xmlFile
     *            the xml file
     * @return the document
     * @throws ParserConfigurationException
     *             the parser configuration exception
     * @throws SAXException
     *             the sAX exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     * @see #createDocument(String)
     */
    public static Document createDocument(File xmlFile)
            throws ParserConfigurationException, SAXException, IOException {
        return createDocument(getFileChars(xmlFile));
    }

    /**
     * read a text file.
     * 
     * @param file
     *            the file
     * @return the file contents as a String
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static String getFileChars(File file) throws IOException {
        return new String(getFileBytes(file));
    }

    /**
     * Gets the file chars.
     * 
     * @param fileName
     *            the file name
     * @return the file chars
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     * @see #getFileChars(File)
     */
    public static String getFileChars(String fileName) throws IOException {
        return getFileChars(new File(fileName));
    }

    /**
     * read the bytes of a file, mostly for reading jar files.
     * 
     * @param file
     *            the file
     * @return the file contents as a byte array
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static byte[] getFileBytes(File file) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
        byte buf[] = new byte[8192];
        int ret = 0;
        while ((ret = fin.read(buf)) != -1) {
            bout.write(buf, 0, ret);
        }
        fin.close();
        return bout.toByteArray();
    }

    /**
     * Gets the bytes of an inputstream.
     * 
     * @param is
     *            the is
     * @return the file bytes
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static byte[] getFileBytes(InputStream is) throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        BufferedInputStream fin = new BufferedInputStream(is);
        byte buf[] = new byte[8192];
        int ret = 0;
        while ((ret = fin.read(buf)) != -1) {
            bout.write(buf, 0, ret);
        }
        fin.close();
        return bout.toByteArray();
    }
}

Related

  1. createDocument(String rootElementName)
  2. createDocument(String rootName)
  3. createDocument(String rootNodeName)
  4. createDocument(String rootQName, String namespaceURI, String doctypePublicID, String doctypeSystemID)
  5. createDocument(String str)
  6. createDocumentBuilder()
  7. createDocumentBuilder(boolean namespaces, boolean validating)
  8. createDocumentBuilderFactory()
  9. createDocumentBuilderFactory(ClassLoader classLoader)