Java XML Document Create createDocumentFromXmlString(String xmlStr)

Here you can find the source of createDocumentFromXmlString(String xmlStr)

Description

create Document From Xml String

License

Open Source License

Parameter

Parameter Description
xmlStr a parameter

Return

the Document or null upon error.

Declaration

public static Document createDocumentFromXmlString(String xmlStr) 

Method Source Code


//package com.java2s;
/*//w w  w  .  j  a  va2 s  . co m
 * Copyright (c) 2004-2013 Universidade do Porto - Faculdade de Engenharia
 * Laborat?rio de Sistemas e Tecnologia Subaqu?tica (LSTS)
 * All rights reserved.
 * Rua Dr. Roberto Frias s/n, sala I203, 4200-465 Porto, Portugal
 *
 * This file is part of Neptus, Command and Control Framework.
 *
 * Commercial Licence Usage
 * Licencees holding valid commercial Neptus licences may use this file
 * in accordance with the commercial licence agreement provided with the
 * Software or, alternatively, in accordance with the terms contained in a
 * written agreement between you and Universidade do Porto. For licensing
 * terms, conditions, and further information contact lsts@fe.up.pt.
 *
 * European Union Public Licence - EUPL v.1.1 Usage
 * Alternatively, this file may be used under the terms of the EUPL,
 * Version 1.1 only (the "Licence"), appearing in the file LICENCE.md
 * included in the packaging of this file. You may not use this work
 * except in compliance with the Licence. Unless required by applicable
 * law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF
 * ANY KIND, either express or implied. See the Licence for the specific
 * language governing permissions and limitations at
 * https://www.lsts.pt/neptus/licence.
 *
 * For more information please see <http://lsts.fe.up.pt/neptus>.
 *
 * Author: Paulo Dias
 * 7 de Jul de 2011
 */

import java.io.ByteArrayInputStream;

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

import org.w3c.dom.Document;

public class Main {
    /**
     * @param xmlStr
     * @return the Document or null upon error.
     */
    public static Document createDocumentFromXmlString(String xmlStr) {
        try {
            DocumentBuilderFactory docBuilderFactory;
            docBuilderFactory = DocumentBuilderFactory.newInstance();
            docBuilderFactory.setIgnoringComments(true);
            docBuilderFactory.setIgnoringElementContentWhitespace(true);
            docBuilderFactory.setNamespaceAware(false);
            DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
            ByteArrayInputStream bais = new ByteArrayInputStream(xmlStr.getBytes());
            Document doc = builder.parse(bais);
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. createDocumentFromString(String str)
  2. createDocumentFromString(String xmlString)
  3. createDocumentFromXml(String input)
  4. createDocumentFromXMLContent(String docContent)
  5. createDocumentFromXmlFragment(final InputStream inputStreamXmlFragment, final URL urlSchema)
  6. createEmptyDocument()
  7. createEmptyDocument()
  8. createEmptyDocument()
  9. createEmptyDocument()