Пример #1
0
  public static String transformer(InputStream xml, InputStream xsl, boolean print) {
    StreamSource stylesource = new StreamSource(xsl);
    TransformerFactory factory = TransformerFactory.newInstance();
    javax.xml.transform.Transformer transformer;

    try {

      StreamSource source = new StreamSource(xml);
      transformer = factory.newTransformer(stylesource);
      StringWriter writer = new StringWriter();
      StreamResult result = new StreamResult(writer);
      transformer.transform(source, result);
      String response = writer.toString();
      if (print) {
        System.out.println(response);
      }
      return response;

    } catch (TransformerConfigurationException ex) {
    } catch (TransformerException ex) {
    }
    return "";
  }
Пример #2
0
  public static boolean writeXML(File file, Document document) {

    javax.xml.transform.Transformer transformer = null;
    try {
      transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
      e.printStackTrace();
      return false;
    }

    transformer.setOutputProperty("indent", "yes");
    transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "2");
    transformer.setOutputProperty("encoding", "UTF-8");

    try {
      transformer.transform(new DOMSource(document), new StreamResult(file));
    } catch (TransformerException e) {
      e.printStackTrace();
      return false;
    }

    return true;
  }