Java XML Document Create stringToDocument(String xmlString)

Here you can find the source of stringToDocument(String xmlString)

Description

string To Document

License

Open Source License

Declaration

public static Document stringToDocument(String xmlString) throws IOException, SAXException 

Method Source Code


//package com.java2s;
/*//from   w  w  w  .  ja v a  2 s .  c  om
 * Copyright (C) 2018 Saxon State and University Library Dresden (SLUB)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;

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

public class Main {
    @SuppressWarnings("CanBeFinal") // initialization happens in static block
    private static DocumentBuilder db;

    public static Document stringToDocument(String xmlString) throws IOException, SAXException {
        return db.parse(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
    }

    public static Document parse(InputStream inputStream) throws IOException, SAXException {
        Document doc = db.parse(inputStream);
        doc.getDocumentElement().normalize();
        return doc;
    }
}

Related

  1. stringToDocument(String string)
  2. stringToDocument(String string)
  3. stringToDocument(String string)
  4. stringToDocument(String xml)
  5. stringToDocument(String xmlStr, boolean isValidating)
  6. toDocument(File f)
  7. toDocument(Reader reader)
  8. toDocument(ResultSet rs)
  9. toDocument(SOAPMessage soapMsg)