本文整理了Java中javax.xml.transform.Transformer.transform()
方法的一些代码示例,展示了Transformer.transform()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Transformer.transform()
方法的具体详情如下:
包路径:javax.xml.transform.Transformer
类名称:Transformer
方法名:transform
[英]Transform the XML Source
to a Result
. Specific transformation behavior is determined by the settings of the TransformerFactory
in effect when the Transformer
was instantiated and any modifications made to the Transformer
instance.
An empty Source
is represented as an empty document as constructed by javax.xml.parsers.DocumentBuilder#newDocument(). The result of transforming an empty Source
depends on the transformation behavior; it is not always an empty Result
.
[中]将XMLSource
转换为Result
。具体的转换行为由Transformer
实例化时有效的TransformerFactory
设置以及对Transformer
实例所做的任何修改决定。
空的Source
表示为javax构造的空文档。xml。解析器。DocumentBuilder#newDocument()。转换空Source
的结果取决于转换行为;它并不总是空的Result
。
代码示例来源:origin: spring-projects/spring-framework
/**
* Transforms the given {@code Source} to the {@code Result}.
* @param source the source to transform from
* @param result the result to transform to
* @throws TransformerException in case of transformation errors
*/
protected void transform(Source source, Result result) throws TransformerException {
this.transformerFactory.newTransformer().transform(source, result);
}
代码示例来源:origin: alibaba/fastjson
private static String toString(org.w3c.dom.Node node) {
try {
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
DOMSource domSource = new DOMSource(node);
StringWriter out = new StringWriter();
transformer.transform(domSource, new StreamResult(out));
return out.toString();
} catch (TransformerException e) {
throw new JSONException("xml node to string error", e);
}
}
代码示例来源:origin: jenkinsci/jenkins
/**
* potentially unsafe XML transformation.
* @param source The XML input to transform.
* @param out The Result of transforming the <code>source</code>.
*/
private static void _transform(Source source, Result out) throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
// this allows us to use UTF-8 for storing data,
// plus it checks any well-formedness issue in the submitted data.
Transformer t = factory.newTransformer();
t.transform(source, out);
}
代码示例来源:origin: apache/geode
private static String transform(Transformer transformer, Node element)
throws TransformerException {
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(element);
transformer.transform(source, result);
return result.getWriter().toString();
}
代码示例来源:origin: gocd/gocd
private String upgrade(String originalContent, URL upgradeScript) {
try (InputStream xslt = upgradeScript.openStream()) {
ByteArrayOutputStream convertedConfig = new ByteArrayOutputStream();
transformer(upgradeScript.getPath(), xslt)
.transform(new StreamSource(new ByteArrayInputStream(originalContent.getBytes())), new StreamResult(convertedConfig));
return convertedConfig.toString();
} catch (TransformerException e) {
throw bomb("Couldn't transform configuration file using upgrade script " + upgradeScript.getPath(), e);
} catch (IOException e) {
throw bomb("Couldn't write converted config file", e);
}
}
代码示例来源: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: 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: pmd/pmd
private void transform(Document doc) {
DOMSource source = new DOMSource(doc);
this.setWriter(new StringWriter());
StreamResult result = new StreamResult(this.outputWriter);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
e.printStackTrace();
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void whitespace() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test><node1> </node1><node2> Some text </node2></test>";
Transformer transformer = TransformerFactory.newInstance().newTransformer();
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(
new ByteArrayInputStream(xml.getBytes("UTF-8")));
SAXSource source = new SAXSource(staxXmlReader, new InputSource());
DOMResult result = new DOMResult();
transformer.transform(source, result);
Node node1 = result.getNode().getFirstChild().getFirstChild();
assertEquals(" ", node1.getTextContent());
assertEquals(" Some text ", node1.getNextSibling().getTextContent());
}
代码示例来源:origin: spring-projects/spring-framework
private void transform(Source source, Result result) throws TransformerException {
this.transformerFactory.newTransformer().transform(source, result);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void streamReaderSourceToStreamResult() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader);
assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
assertNull("EventReader returned", source.getXMLEventReader());
StringWriter writer = new StringWriter();
transformer.transform(source, new StreamResult(writer));
assertThat("Invalid result", writer.toString(), isSimilarTo(XML));
}
代码示例来源: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
public void createXml(OutputStream os) throws TransformerException, ParserConfigurationException {
final DOMSource source = new DOMSource(getDocument());
final StreamResult scrResult = new StreamResult(os);
getTransformer().transform(source, scrResult);
}
代码示例来源:origin: org.springframework/spring-web
/**
* Transforms the given {@code Source} to the {@code Result}.
* @param source the source to transform from
* @param result the result to transform to
* @throws TransformerException in case of transformation errors
*/
protected void transform(Source source, Result result) throws TransformerException {
this.transformerFactory.newTransformer().transform(source, result);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void eventReaderSourceToStreamResult() throws Exception {
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
StaxSource source = new StaxSource(eventReader);
assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
assertNull("StreamReader returned", source.getXMLStreamReader());
StringWriter writer = new StringWriter();
transformer.transform(source, new StreamResult(writer));
assertThat("Invalid result", writer.toString(), isSimilarTo(XML));
}
代码示例来源: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: spring-projects/spring-framework
private void transformAndMarshal(Object graph, Result result) throws IOException {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
marshalOutputStream(graph, os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
Transformer transformer = this.transformerFactory.newTransformer();
transformer.transform(new StreamSource(is), result);
}
catch (TransformerException ex) {
throw new MarshallingFailureException(
"Could not transform to [" + ClassUtils.getShortName(result.getClass()) + "]", ex);
}
}
代码示例来源: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: iBotPeaches/Apktool
/**
*
* @param file File to save Document to (ie AndroidManifest.xml)
* @param doc Document being saved
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws TransformerException
*/
private static void saveDocument(File file, Document doc)
throws IOException, SAXException, ParserConfigurationException, TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
}
代码示例来源: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));
内容来源于网络,如有侵权,请联系作者删除!