Android XML Element Child Get getStringFromXml(Element validate, Boolean hasHeader)

Here you can find the source of getStringFromXml(Element validate, Boolean hasHeader)

Description

get String From Xml

License

Apache License

Declaration

public static String getStringFromXml(Element validate,
            Boolean hasHeader) 

Method Source Code

//package com.java2s;
/*// w w  w.  j  ava  2s.co m
 * Copyright 2013 Hewlett-Packard Development Company, L.P
 *
 * 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 javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
import java.io.StringWriter;
import org.w3c.dom.Element;

public class Main {
    public static String getStringFromXml(Element validate,
            Boolean hasHeader) {
        final String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = transFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
        StringWriter buffer = new StringWriter();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                "yes");
        try {
            transformer.transform(new DOMSource(validate),
                    new StreamResult(buffer));
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        String s = buffer.toString();
        if (hasHeader) {
            return header + s;
        }
        return s;
    }
}

Related

  1. valueOfFirstDescendantWithName(Element element, String name)
  2. firstDescendantWithName(Element element, String name)
  3. findNextSiblingElement(Element current)
  4. GetElement(Element root, String namespaceUri, String name, boolean throwIfNotFound)
  5. getListContent(Element listElement, String listItemName, String listItemNamespace)