Java XML Element to String toString(Element xml)

Here you can find the source of toString(Element xml)

Description

to String

License

Open Source License

Declaration

private static String toString(Element xml) 

Method Source Code

//package com.java2s;
/*//from ww  w  .ja  v a 2 s .c om
 *
 *  Copyright (c) 2014 Cisco Systems, Inc. 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
 *
 */

import org.w3c.dom.Element;

import javax.xml.transform.OutputKeys;
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.util.Iterator;

public class Main {
    private static String toString(Element xml) {
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(xml);
            transformer.transform(source, result);

            return result.getWriter().toString();
        } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
            throw new RuntimeException("Unable to serialize xml element " + xml, e);
        }
    }

    private static String toString(Iterable<Element> xmlIterable) {
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            StreamResult result = new StreamResult(new StringWriter());
            Iterator iterator = xmlIterable.iterator();
            DOMSource source;
            if (iterator.hasNext()) {
                source = new DOMSource((org.w3c.dom.Node) iterator.next());
                transformer.transform(source, result);
                transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            }

            while (iterator.hasNext()) {
                source = new DOMSource((org.w3c.dom.Node) iterator.next());
                transformer.transform(source, result);
            }
            System.out.println(result.getWriter().toString());
            return result.getWriter().toString();
        } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
            throw new RuntimeException("Unable to serialize xml element(s) " + xmlIterable.toString(), e);
        }
    }
}

Related

  1. getXmlString(Element node)
  2. getXMLStringFromNode(Element node)
  3. toString(Element e)
  4. toString(Element element)
  5. toString(Element node)
  6. toStringNoIndent(Element aElement)