Parses a String into a new XML Element. - Java XML

Java examples for XML:XML Element Value

Description

Parses a String into a new XML Element.

Demo Code

/**/*from www .j a v  a2  s.  c  o  m*/
 * Copyright 2012 Jos? Mart?nez
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.DOMResult;
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.w3c.dom.Element;

public class Main{
    public static void main(String[] argv) throws Exception{
        String element = "java2s.com";
        System.out.println(fromString(element));
    }
    private static final DocumentBuilder docBuilder;
    private static final Transformer transformer;
    /**
     * Parses a String into a new Element.
     * 
     * @param element the String to parse
     * @return the parsed Element
     */
    public static final Element fromString(final String element) {
        try {
            final Document doc = newDocument();
            transformer.transform(new StreamSource(
                    new StringReader(element)), new DOMResult(doc));
            return doc.getDocumentElement();
        } catch (final TransformerException e) {
            throw new InternalError("Transformer error");
        }
    }
    /**
     * Returns a new Document from the current DocumentBuidler.
     * 
     * @return a new Document
     */
    public static final Document newDocument() {
        return docBuilder.newDocument();
    }
}

Related Tutorials