本文整理汇总了Java中javax.xml.transform.TransformerFactory.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java TransformerFactory.setAttribute方法的具体用法?Java TransformerFactory.setAttribute怎么用?Java TransformerFactory.setAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.TransformerFactory
的用法示例。
在下文中一共展示了TransformerFactory.setAttribute方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPayload
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
@Override
public String [] getPayload (final PayloadType type, final String body) {
final Source input = new StreamSource (new StringReader (body));
final StringWriter writer = new StringWriter ();
final StreamResult output = new StreamResult (writer);
final TransformerFactory transformerFactory = TransformerFactory.newInstance ();
transformerFactory.setAttribute ("indent-number", 4);
try {
final Transformer transformer = transformerFactory.newTransformer ();
transformer.setOutputProperty (OutputKeys.INDENT, "yes");
transformer.transform (input, output);
return output.getWriter ()
.toString ()
.split ("\n");
}
catch (final TransformerException e) {
fail (XmlFormatTransformerError.class, "Error while Xml Transformation.", e);
}
return new String [] {};
}
示例2: makeTransformer
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Helper to make an XML Transformer.
*
* @param declaration If true, include the XML declaration.
* @param indent If true, set up the transformer to indent.
* @return A suitable {@code Transformer}.
*/
public static Transformer makeTransformer(boolean declaration,
boolean indent) {
Transformer tf = null;
try {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", new Integer(2));
tf = factory.newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
if (!declaration) {
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}
if (indent) {
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
} catch (TransformerException e) {
logger.log(Level.WARNING, "Failed to install transformer!", e);
}
return tf;
}
示例3: getTransformerFactory
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Returns an instance of TransformerFactory with either a custom URIResolver
* or Catalog.
*
* @param setUseCatalog a flag indicates whether USE_CATALOG shall be set
* through the factory
* @param useCatalog the value of USE_CATALOG
* @param catalog a catalog
* @param resolver a custom resolver
* @return an instance of TransformerFactory
* @throws Exception
*/
TransformerFactory getTransformerFactory(boolean setUseCatalog, boolean useCatalog,
String catalog, URIResolver resolver)
throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
if (setUseCatalog) {
factory.setFeature(XMLConstants.USE_CATALOG, useCatalog);
}
if (catalog != null) {
factory.setAttribute(CatalogFeatures.Feature.FILES.getPropertyName(), catalog);
}
// use resolver or catalog if resolver = null
if (resolver != null) {
factory.setURIResolver(resolver);
}
return factory;
}
示例4: printXmlDoc
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
public String printXmlDoc(Document doc) throws Exception {
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
TransformerFactory transformerFact = TransformerFactory.newInstance();
transformerFact.setAttribute("indent-number", new Integer(4));
Transformer transformer;
transformer = transformerFact.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");
transformer.transform(new DOMSource(doc), result);
return sw.toString();
}
示例5: NodesToString
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Transforms a list of nodes into their XML string representation
*
* @param ns List of nodes
* @param verbose Flag to print a comment with the number of output nodes and before each one
* @return String containing the XML plaintext representations of the nodes (without a common root)
* @throws Exception Internal error
*/
public static String NodesToString(List<Node> ns, boolean verbose) throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
// Set indentation to two spaces
tf.setAttribute("indent-number", 2);
Transformer ts = tf.newTransformer();
// Standard XML document
ts.setOutputProperty(OutputKeys.METHOD, "xml");
ts.setOutputProperty(OutputKeys.STANDALONE, "yes");
// Encoding
ts.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// Indent results
ts.setOutputProperty(OutputKeys.INDENT, "yes");
// Do not include xml declaration if there is more than one root node
if (verbose || (ns.size() != 1)) {
ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
} else {
ts.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
}
// Join the output of each node
String output = "";
if (verbose) {
output += "<!-- Number of nodes: " + ns.size() + " -->\n";
}
for (int i = 0; i < ns.size(); ++i) {
if (verbose) {
output += "<!-- Node #" + (i + 1) + " -->\n";
}
output += IO.NodeToString(ns.get(i), ts) + "\n";
}
return output;
}
示例6: saveXml
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
public void saveXml(OutputStream outputStream, Document doc) throws Exception {
TransformerFactory transFactory = TransformerFactory.newInstance();
transFactory.setAttribute("indent-number", new Integer(3));
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0 ");
PrintWriter printWriter = new PrintWriter(outputStream);
transformer.transform(new DOMSource(doc), new StreamResult(printWriter));
}
示例7: testDocument
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* @bug 8062518 8153082
* Verifies that a reference to the DTM created by XSLT document function is
* actually read from the DTM by an extension function.
* @param xml Content of xml file to process
* @param xsl stylesheet content that loads external document {@code externalDoc}
* with XSLT 'document' function and then reads it with
* DocumentExtFunc.test() function
* @param externalDoc Content of the external xml document
* @param expectedResult Expected transformation result
**/
@Test(dataProvider = "document")
public void testDocument(final String xml, final String xsl,
final String externalDoc, final String expectedResult) throws Exception {
// Prepare sources for transormation
Source src = new StreamSource(new StringReader(xml));
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf = TransformerFactory.newInstance();
tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
tf.setAttribute(EXTENSION_CLASS_LOADER,
runWithAllPerm(() -> Thread.currentThread().getContextClassLoader()));
Transformer t = tf.newTransformer( xslsrc );
t.setErrorListener(tf.getErrorListener());
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver(new URIResolver() {
@Override
public Source resolve(String href, String base)
throws TransformerException {
if (href.contains("externalDoc")) {
return new StreamSource(new StringReader(externalDoc));
} else {
return new StreamSource(new StringReader(xml));
}
}
});
// Prepare output stream
StringWriter xmlResultString = new StringWriter();
StreamResult xmlResultStream = new StreamResult(xmlResultString);
//Transform the xml
t.transform(src, xmlResultStream);
// If the document can't be accessed and the bug is in place then
// reported exception will be thrown during transformation
System.out.println("Transformation result:"+xmlResultString.toString().trim());
// Check the result - it should contain two (node name, node values) entries -
// one for original document, another for a document created with
// call to 'document' function
assertEquals(xmlResultString.toString().trim(), expectedResult);
}