Java XML Element from String stringToElement(String s)

Here you can find the source of stringToElement(String s)

Description

Convert the String representation of an Element into an Element

License

Open Source License

Parameter

Parameter Description
String representation of an Element.

Return

Element

Declaration

public static Element stringToElement(String s)
        throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from  w  ww.jav  a 2  s  .co m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class Main {
    public static final String UTF8_ENCODING = "UTF-8";

    /**
     * Convert the String representation of an Element into an Element
     * @param String representation of an Element.
     * @return Element
     */
    public static Element stringToElement(String s)
            throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException {
        return stringToElement(s, false);
    }

    /**
     * Convert the String representation of an Element into an Element
     * @param String representation of an Element.
     * @param boolean set whether the return Element should be namespace aware.
     * @return Element
     */
    public static Element stringToElement(String s, boolean namespaceAware)
            throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException {
        return byteArrayToElement(s.getBytes(UTF8_ENCODING), namespaceAware);
    }

    /**
     * Convert the byte array representation of an Element into an Element
     * @param byte[] representation of an Element.
     * @param boolean set whether the return Element should be namespace aware.
     * @return Element
     */
    public static Element byteArrayToElement(byte[] b, boolean namespaceAware)
            throws ParserConfigurationException, SAXException, UnsupportedEncodingException, IOException {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setNamespaceAware(namespaceAware);
        docBuilderFactory.setValidating(false);
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(new ByteArrayInputStream(b));
        return doc.getDocumentElement();
    }
}

Related

  1. stringToElement(final String inputXml)
  2. stringToElement(String xml)