javax.xml.transform.Transformer.setOutputProperty()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(193)

本文整理了Java中javax.xml.transform.Transformer.setOutputProperty()方法的一些代码示例,展示了Transformer.setOutputProperty()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Transformer.setOutputProperty()方法的具体详情如下:
包路径:javax.xml.transform.Transformer
类名称:Transformer
方法名:setOutputProperty

Transformer.setOutputProperty介绍

[英]Set an output property that will be in effect for the transformation.

Pass a qualified property name as a two-part string, the namespace URI enclosed in curly braces ({}), followed by the local name. If the name has a null URL, the String only contain the local name. An application can safely check for a non-null URI by testing to see if the first character of the name is a '{' character.

For example, if a URI and local name were obtained from an element defined with <xyz:foo xmlns:xyz="http://xyz.foo.com/yada/baz.html"/>, then the qualified name would be "{http://xyz.foo.com/yada/baz.html}foo". Note that no prefix is used.

The Properties object that was passed to #setOutputPropertieswon't be effected by calling this method.
[中]设置将对转换生效的输出属性。
将限定属性名作为两部分字符串传递,名称空间URI用大括号({})括起来,后跟本地名称。如果名称的URL为空,则字符串仅包含本地名称。应用程序可以通过测试名称的第一个字符是否为“{”字符来安全地检查非空URI。
例如,如果从使用<xyz:foo xmlns:xyz=”定义的元素中获取URI和本地名称http://xyz.foo.com/yada/baz.html“/>,则限定名为”{http://xyz.foo.com/yada/baz.html}注意,没有使用前缀。
调用此方法不会影响传递给#SetOutputProperties的Properties对象。

代码示例

代码示例来源:origin: stackoverflow.com

public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
  TransformerFactory tf = TransformerFactory.newInstance();
  Transformer transformer = tf.newTransformer();
  transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
  transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

  transformer.transform(new DOMSource(doc), 
     new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}

代码示例来源:origin: spring-projects/spring-integration-samples

public static final Transformer createIndentingTransformer() {
  Transformer xformer;
  try {
    xformer = transformerFactory.newTransformer();
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(2));
  } catch (Exception ex) {
    throw new IllegalStateException(ex);
  }
  xformer.setOutputProperty(OutputKeys.INDENT, "yes");
  xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
  return xformer;
}

代码示例来源:origin: plantuml/plantuml

private Transformer getTransformer() throws TransformerException {
  final TransformerFactory xformFactory = TransformerFactory.newInstance();
  final Transformer transformer = xformFactory.newTransformer();
  transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
  return transformer;
}

代码示例来源:origin: spring-projects/spring-integration-samples

public static final void writeXml(Document document) {
  Transformer transformer = createIndentingTransformer();
  transformer.setOutputProperty(OutputKeys.METHOD, "xml");
  try {
    StringResult streamResult = new StringResult();
    transformer.transform(new DOMSource(document), streamResult);
  } catch (Exception ex) {
    throw new IllegalStateException(ex);
  }
}
public static final Transformer createIndentingTransformer() {

代码示例来源:origin: yasserg/crawler4j

/**
 * Returns a transformer handler that serializes incoming SAX events to
 * XHTML or HTML (depending the given method) using the given output encoding.
 *
 * @param encoding output encoding, or <code>null</code> for the platform default
 */
private static TransformerHandler getTransformerHandler(OutputStream out, String method,
                            String encoding)
  throws TransformerConfigurationException {
  TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
  Transformer transformer = transformerHandler.getTransformer();
  transformer.setOutputProperty(OutputKeys.METHOD, method);
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  if (encoding != null) {
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
  }
  transformerHandler.setResult(new StreamResult(new PrintStream(out)));
  return transformerHandler;
}

代码示例来源:origin: stackoverflow.com

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);

代码示例来源:origin: apache/geode

/****
 * Converts the document to a well formatted Xml string
 *
 * @return pretty xml string
 */
public static String prettyXml(Node doc)
  throws TransformerFactoryConfigurationError, TransformerException {
 Transformer transformer = TransformerFactory.newInstance().newTransformer();
 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 return transform(transformer, doc);
}

代码示例来源:origin: stackoverflow.com

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node),
   new StreamResult(buffer));
String str = buffer.toString();

代码示例来源:origin: plantuml/plantuml

private Transformer getTransformer() throws TransformerException {
  // Get a TransformerFactory object.
  TransformerFactory xformFactory = null;
  try {
    final Class<?> factoryClass = Class
        .forName("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
    xformFactory = (TransformerFactory) factoryClass.newInstance();
  } catch (Exception e) {
    xformFactory = TransformerFactory.newInstance();
  }
  Log.info("TransformerFactory=" + xformFactory.getClass());
  // Get an XSL Transformer object.
  final Transformer transformer = xformFactory.newTransformer();
  Log.info("Transformer=" + transformer.getClass());
  // // Sets the standalone property in the first line of
  // // the output file.
  transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
  // transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "SVG 1.1");
  // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
  return transformer;
}

代码示例来源:origin: pmd/pmd

private void dumpDocToWriter(Document doc, Writer writer) {
  try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "codefragment");
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
  } catch (TransformerException e) {
    throw new IllegalStateException(e);
  }
}

代码示例来源:origin: pentaho/pentaho-kettle

@Override
public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
 meta = (XMLJoinMeta) smi;
 data = (XMLJoinData) sdi;
 if ( !super.init( smi, sdi ) ) {
  return false;
 }
 try {
  Transformer transformer = TransformerFactory.newInstance().newTransformer();
  if ( meta.getEncoding() != null ) {
   transformer.setOutputProperty( OutputKeys.ENCODING, meta.getEncoding() );
  }
  if ( meta.isOmitXMLHeader() ) {
   transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
  }
  transformer.setOutputProperty( OutputKeys.INDENT, "no" );
  setTransformer( transformer );
  // See if a main step is supplied: in that case move the corresponding rowset to position 0
  //
  swapFirstInputRowSetIfExists( meta.getTargetXMLstep() );
 } catch ( Exception e ) {
  log.logError( BaseMessages.getString( PKG, "XMLJoin.Error.Init" ), e );
  return false;
 }
 return true;
}

代码示例来源:origin: stackoverflow.com

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String output = writer.getBuffer().toString().replaceAll("\n|\r", "");

代码示例来源:origin: pentaho/pentaho-kettle

public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
 meta = (AddXMLMeta) smi;
 data = (AddXMLData) sdi;
 if ( !super.init( smi, sdi ) ) {
  return false;
 }
 try {
  setSerializer( TransformerFactory.newInstance().newTransformer() );
  setDomImplentation( XMLParserFactoryProducer.createSecureDocBuilderFactory().newDocumentBuilder().getDOMImplementation() );
  if ( meta.getEncoding() != null ) {
   getSerializer().setOutputProperty( OutputKeys.ENCODING, meta.getEncoding() );
  }
  if ( meta.isOmitXMLheader() ) {
   getSerializer().setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
  }
 } catch ( TransformerConfigurationException e ) {
  return false;
 } catch ( ParserConfigurationException e ) {
  return false;
 }
 return true;
}

代码示例来源:origin: apache/hive

private void writeToFile(File template, Document document) throws Exception {
 Transformer transformer = TransformerFactory.newInstance().newTransformer();
 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
 DOMSource source = new DOMSource(document);
 StreamResult result = new StreamResult(template);
 transformer.transform(source, result);
}

代码示例来源:origin: apache/hive

private void writeToFile(File template, Document document) throws TransformerException {
 Transformer transformer = TransformerFactory.newInstance().newTransformer();
 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
 DOMSource source = new DOMSource(document);
 StreamResult result = new StreamResult(template);
 transformer.transform(source, result);
}

代码示例来源:origin: stackoverflow.com

public static String toString(Document doc) {
  try {
    StringWriter sw = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

    transformer.transform(new DOMSource(doc), new StreamResult(sw));
    return sw.toString();
  } catch (Exception ex) {
    throw new RuntimeException("Error converting to String", ex);
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) {
 try {
  Transformer serializer = tFactory.newTransformer();
  if (prettyPrint) {
   //Setup indenting to "pretty print"
   serializer.setOutputProperty(OutputKeys.INDENT, "yes");
   serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
  }
  if ( ! includeXmlDeclaration) {
   serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  }
  DOMSource xmlSource = new DOMSource(node);
  StreamResult outputTarget = new StreamResult(out);
  serializer.transform(xmlSource, outputTarget);
 } catch (TransformerException e) {
  throw new RuntimeException(e);
 }
}

代码示例来源:origin: plantuml/plantuml

public void transformerXml(OutputStream os) throws TransformerException, ParserConfigurationException {
  final Source source = new DOMSource(document);
  final Result resultat = new StreamResult(os);
  final TransformerFactory fabrique = TransformerFactory.newInstance();
  final Transformer transformer = fabrique.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  transformer.transform(source, resultat);
}

代码示例来源:origin: plantuml/plantuml

public void transformerXml(OutputStream os) throws TransformerException, ParserConfigurationException {
  final Source source = new DOMSource(document);
  final Result resultat = new StreamResult(os);
  final TransformerFactory fabrique = TransformerFactory.newInstance();
  final Transformer transformer = fabrique.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  // transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
  transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  // tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  transformer.transform(source, resultat);
}

代码示例来源:origin: plantuml/plantuml

final public void transformerXml(OutputStream os) throws TransformerException, ParserConfigurationException {
  final Source source = new DOMSource(document);
  final Result resultat = new StreamResult(os);
  final TransformerFactory fabrique = TransformerFactory.newInstance();
  final Transformer transformer = fabrique.newTransformer();
  transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  // transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
  transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  // tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
  transformer.transform(source, resultat);
}

相关文章