Java XML Element Create createElement(String name, Object value, Document doc)

Here you can find the source of createElement(String name, Object value, Document doc)

Description

create Element

License

Open Source License

Declaration

public static Element createElement(String name, Object value, Document doc) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.StringWriter;
import java.io.Writer;

public class Main {
    public static Element createElement(String name, Object value, Document doc) {
        Element element = doc.createElement(name);
        element.appendChild(doc.createTextNode(value.toString()));
        return element;
    }/*from   www.j  a v  a 2 s . co  m*/

    public static String toString(Document doc, String encoding, boolean indent)
            throws TransformerFactoryConfigurationError, TransformerException {
        Source source = new DOMSource(doc);
        StringWriter sw = new StringWriter();
        Result result = new StreamResult(sw);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

        if (indent) {
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        }

        xformer.transform(source, result);

        return sw.getBuffer().toString();
    }

    public static void toString(Document doc, String encoding, Writer w, boolean indent)
            throws TransformerFactoryConfigurationError, TransformerException {
        Source source = new DOMSource(doc);
        Result result = new StreamResult(w);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

        if (indent) {
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        }

        xformer.transform(source, result);
    }
}

Related

  1. createElement(Node element, String name)
  2. createElement(Node node, String name)
  3. createElement(Node parent, String tag)
  4. createElement(QName key)
  5. createElement(QName qName)
  6. createElement(String tag, String textContent, Document xml)
  7. createElement(String tagName, String text, Document doc)
  8. createElementAndText(Element parent, String elementName, String text)
  9. createElementInSameNamespace(Element parent, String localName)