Return the XMLDocument formatted as a String

@param doc the XML Document
@return A String representation of the XML Document
@throws javax.xml.transform.TransformerConfigurationException
@throws javax.xml.transform.TransformerException
974607
public static String getDocumentAsXmlString(Document doc) throws TransformerConfigurationException, TransformerException {
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        try {
            tf.setAttribute("indent-number", 4);
        } catch (IllegalArgumentException e) {
            // ignore
        }
        Transformer transformer = tf.newTransformer();
        //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        //
        java.io.StringWriter sw = new java.io.StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);
        return sw.toString();
    }
715323
public static String docToString1(Document dom) {
        StringWriter sw = new StringWriter();
        DOM2Writer.serializeAsXML(dom, sw);
        return sw.toString();
    }
18679
@Override
	public Document readXML() {
		final String xmlString = readString();
		Document doc = null;
		try {
			doc = XMLUtils.stringToDoc(xmlString);
		} catch (IOException ioex) {
			log.error("IOException converting xml to dom", ioex);
		}
		storeReference(doc);
		return doc;
	}
509807
public static String toStr(Document doc, boolean isPretty) {
		return toStr(doc, CharsetUtil.UTF_8, isPretty);
	}
107941
public static Document convertXMLToDOM(String strXML)
    {
        DocumentBuilder db = Util.getDocumentBuilder();
        // Parse the input file
        Document doc = null;
        try {
            synchronized (db)
            {   // db is not thread safe
                doc = db.parse(new InputSource(new StringReader(strXML)));
            }
        } catch (SAXException se) {
            System.out.println(se.getMessage());
            return null;
        } catch (IOException ioe) {
            System.out.println(ioe);
            return null;
        } catch (Exception ioe) {
            System.out.println(ioe);
            return null;
        }
        return doc;
    }
1372325
public static Document getDocument(InputStream inputStream) {
        Document result = null;

        try {
            // memory optimization over minor performance sacrifice
            synchronized (XmlRepositoryFactory.class) {
                if (documentBuilder == null) {
                    documentBuilder = builderFactory.newDocumentBuilder();
                }

                result = documentBuilder.parse(inputStream);
            }
        } catch (ParserConfigurationException ex) {
            throw new MjdbcRuntimeException(ex);
        } catch (SAXException ex) {
            throw new MjdbcRuntimeException(ex);
        } catch (IOException ex) {
            throw new MjdbcRuntimeException(ex);
        }

        return result;
    }
849799
public static String domToString(Node dom, boolean withXmlDeclaration)
            throws Exception {
        Transformer t = XmlTransformUtility.getTransformer();
        t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        if (withXmlDeclaration) {
            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        } else {
            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        }
        t.setOutputProperty(OutputKeys.METHOD, "xml");

        /* Transformer */
        StringWriter sw = new StringWriter();
        t.transform(new DOMSource(dom), new StreamResult(sw));

        return sw.toString();
    }
436173
public static Node normalizeFormatting(final Node doc) throws CouldNotProcessException {
        try {
            return createDocumentFromString(normalizeFormattingAsString(doc));
        } catch (XMLParsingException ex) {
            throw new CouldNotProcessException("Couldn't normalize formatting. Returning old document.", ex);
        }
    }
173024
public void writeXmlDocument(Document document, File file) throws IOException {
        try {
            Transformer tr = transformerFactory.newTransformer();
            tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            tr.setOutputProperty(OutputKeys.INDENT, "yes");
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            tr.transform(new DOMSource(document), new StreamResult(fileOutputStream));
            fileOutputStream.close();
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }
239481
public static Document toDocument(String xml) {
		try {

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
					.newInstance();
			documentBuilderFactory.setNamespaceAware(true);
			documentBuilderFactory.setIgnoringElementContentWhitespace(true);
			documentBuilderFactory.isIgnoringComments();
			documentBuilderFactory.setCoalescing(true);

			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();

			InputSource inputSource = new InputSource();

			inputSource.setCharacterStream(new StringReader(xml));

			Document document = documentBuilder.parse(inputSource);

			removeUnusedNamespaces(document);

			return document;

		} catch (ParserConfigurationException e) {
			throw new RuntimeException(e);
		} catch (SAXException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
977509
public BioCDocument readDocument()
      throws XMLStreamException {

    if (reader.document != null) {
      BioCDocument thisDocument = reader.document;
      reader.read();
      return thisDocument;
    } else {
      return null;
    }
  }
1337601
public static String documentToString(Document xmlDocument) throws IOException {
        String encoding = (xmlDocument.getXmlEncoding() == null) ? "UTF-8" : xmlDocument.getXmlEncoding();
        OutputFormat format = new OutputFormat(xmlDocument);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        format.setEncoding(encoding);
        try (Writer out = new StringWriter()) {
            XMLSerializer serializer = new XMLSerializer(out, format);
            serializer.serialize(xmlDocument);
            return out.toString();
        }
    }
657487
protected static String toStringForTermedDocument(TermedDocument o) {
		StringBuilder sb = new StringBuilder();
		boolean first;

		sb.append("\n* Labels: ");
		first = true;
		SortedSet<String> labelKeys = new TreeSet<>(o.getLabels()
				.keySet());
		for (String key : labelKeys) {
			if (first) {
				first = false;
			} else {
				sb.append("; ");
			}
			sb.append(toString(o.getLabels().get(key)));
		}

		sb.append("\n* Descriptions: ");
		first = true;
		SortedSet<String> descriptionKeys = new TreeSet<>(o
				.getDescriptions().keySet());
		for (String key : descriptionKeys) {
			if (first) {
				first = false;
			} else {
				sb.append("; ");
			}
			sb.append(toString(o.getDescriptions().get(key)));
		}

		sb.append("\n* Aliases: ");
		first = true;
		SortedSet<String> aliasKeys = new TreeSet<>(o.getAliases()
				.keySet());
		for (String key : aliasKeys) {
			for (MonolingualTextValue mtv : o.getAliases().get(key)) {
				if (first) {
					first = false;
				} else {
					sb.append("; ");
				}
				sb.append(toString(mtv));
			}
		}

		return sb.toString();
	}
641875
public static String prettyPrint(String xml) {
        LSParser parser = configurer.createLSParser();
        configurer.setParserConfigParameter(parser, VALIDATE_IF_SCHEMA, false);

        LSInput input = configurer.createLSInput();

        try {
            Charset charset = getTargetCharset(xml);

            input.setByteStream(new ByteArrayInputStream(xml.trim().getBytes(charset)));
            input.setEncoding(charset.displayName());
        } catch(UnsupportedEncodingException e) {
            throw new CitrusRuntimeException(e);
        }

        Document doc;
        try {
            doc = parser.parse(input);
        } catch (Exception e) {
            return xml;
        }

        return serialize(doc);
    }
2131
public String getSortedXml(Document newDocument) {
        try (ByteArrayOutputStream sortedXml = new ByteArrayOutputStream()) {
            BufferedLineSeparatorOutputStream bufferedLineOutputStream =
                    new BufferedLineSeparatorOutputStream(lineSeparatorUtil.toString(), sortedXml);

            XMLOutputter xmlOutputter = new PatchedXMLOutputter(bufferedLineOutputStream, indentBlankLines);
            xmlOutputter.setFormat(createPrettyFormat());
            xmlOutputter.output(newDocument, bufferedLineOutputStream);

            bufferedLineOutputStream.close();
            return sortedXml.toString(encoding);
        } catch (IOException ioex) {
            throw new FailureException("Could not format pom files content", ioex);
        }
    }
475018
public static String formatXml(String xml) {
    assertInitialization();
    return XLog.sLogConfiguration.xmlFormatter.format(xml);
  }
424278
public static void convertDOMToXML(Node doc, Writer out)
    {
        try {
            // Use a Transformer for output
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();

            DOMSource source = new DOMSource(doc);

            StreamResult result = new StreamResult(out);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException tce) {
            // Error generated by the parser
            tce.printStackTrace();
        } catch (TransformerException te) {
            // Error generated by the parser
            te.printStackTrace();
        }
    }