Java XML Document Create createDocumentFromXMLContent(String docContent)

Here you can find the source of createDocumentFromXMLContent(String docContent)

Description

create Document From XML Content

License

Open Source License

Declaration

static public Document createDocumentFromXMLContent(String docContent) throws SAXException,
            ParserConfigurationException, IOException, TransformerConfigurationException, TransformerException 

Method Source Code


//package com.java2s;
/*// w ww  .j  av  a  2 s  .  c o m
 * URLManager - URL Indexer
 * Copyright (C) 2008-2012  Open-S Company
 *
 * This file is part of URLManager.
 *
 * URLManager is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact us by mail: open-s AT open-s DOT com
 */

import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Main {
    static public Document createDocumentFromXMLContent(String docContent) throws SAXException,
            ParserConfigurationException, IOException, TransformerConfigurationException, TransformerException {
        // create builder
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        // create document
        Document doc = docBuilder.parse(new ByteArrayInputStream(docContent.getBytes()));

        // create transformer to remove spaces
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory
                .newTransformer(new StreamSource("src/test/resources/strip-spaces.xls"));

        // load doc
        DOMSource source = new DOMSource(doc);
        OutputStream os = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(os);

        // remove spaces from doc
        transformer.transform(source, result);

        // re-create doc
        return docBuilder.parse(new ByteArrayInputStream(os.toString().getBytes()));
    }
}

Related

  1. createDocumentFromFile(File file)
  2. createDocumentFromResult( final StreamResult result)
  3. createDocumentFromString(String str)
  4. createDocumentFromString(String xmlString)
  5. createDocumentFromXml(String input)
  6. createDocumentFromXmlFragment(final InputStream inputStreamXmlFragment, final URL urlSchema)
  7. createDocumentFromXmlString(String xmlStr)
  8. createEmptyDocument()
  9. createEmptyDocument()