本文整理汇总了Java中javax.xml.transform.Transformer.transform方法的典型用法代码示例。如果您正苦于以下问题:Java Transformer.transform方法的具体用法?Java Transformer.transform怎么用?Java Transformer.transform使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.Transformer
的用法示例。
在下文中一共展示了Transformer.transform方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doTransform
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Performs the XSLT transformation.
*/
private String doTransform(final Transformer transformer, final Source input, final URIResolver resolver)
{
final StringWriter writer = new StringWriter();
final StreamResult output = new StreamResult(writer);
if( resolver != null )
{
transformer.setURIResolver(resolver);
}
try
{
transformer.transform(input, output);
}
catch( final TransformerException ex )
{
throw new RuntimeException("Error transforming XSLT", ex);
}
return writer.toString();
}
示例2: writeXml
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Write out the non-default properties in this configuration to the given {@link java.io.Writer}.
*
* @param out the writer to write to.
*/
public void writeXml(Writer out) throws IOException {
Document doc = asXmlDocument();
try {
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
// Important to not hold Configuration log while writing result, since
// 'out' may be an HDFS stream which needs to lock this configuration
// from another thread.
transformer.transform(source, result);
} catch (TransformerException te) {
throw new IOException(te);
}
}
示例3: prettyPrintXMLString
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
private static void prettyPrintXMLString(String xml,LogLevel level) {
if (isValidXML(xml)) {
log(showline(), level);
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
log(xmlOutput.getWriter().toString().replaceFirst(">", ">\n"), level);
} catch (TransformerException e) {
log("[XML] : Error => Wrong XML Format [ " + e.getMessage() + " ]", LogLevel.ERROR);
}
log(showline(), level);
}
else{
log("[XML] : Error => Wrong XML Format ", LogLevel.WARN);
}
}
示例4: soapMessage2String
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Gets the SOAP message pretty printed as escaped xml for displaying in
* browser
*
* @param msg
* @return
*/
private String soapMessage2String(SOAPMessage msg) {
if (msg == null)
return "";
try (ByteArrayOutputStream streamOut = new ByteArrayOutputStream();) {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
Source sc = msg.getSOAPPart().getContent();
StreamResult result = new StreamResult(streamOut);
transformer.transform(sc, result);
String strMessage = streamOut.toString();
return escapeXmlString(strMessage);
} catch (Exception e) {
System.out.println("Exception in printing SOAP message: "
+ e.getMessage());
return "";
}
}
示例5: printXmlFile
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Outputs the given XML {@link Document} to the file {@code outFile}.
*
* TODO right now reformats the document. Needs to output as-is, respecting white-space.
*
* @param doc The document to output. Must not be null.
* @param outFile The {@link File} where to write the document.
* @param log A log in case of error.
* @return True if the file was written, false in case of error.
*/
static boolean printXmlFile(
@NonNull Document doc,
@NonNull File outFile,
@NonNull IMergerLog log) {
// Quick thing based on comments from http://stackoverflow.com/questions/139076
try {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); //$NON-NLS-1$
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$
tf.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", //$NON-NLS-1$
"4"); //$NON-NLS-1$
tf.transform(new DOMSource(doc), new StreamResult(outFile));
return true;
} catch (TransformerException e) {
log.error(Severity.ERROR,
new FileAndLine(outFile.getName(), 0),
"Failed to write XML file: %1$s",
e.toString());
return false;
}
}
示例6: nodeToString
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Converts the Node/Document/Element instance to XML payload.
*
* @param node the node/document/element instance to convert
* @return the XML payload representing the node/document/element
* @throws AlipayApiException problem converting XML to string
*/
public static String nodeToString(Node node) throws AlipayApiException {
String payload = null;
try {
Transformer tf = TransformerFactory.newInstance().newTransformer();
Properties props = tf.getOutputProperties();
props.setProperty(OutputKeys.INDENT, LOGIC_YES);
props.setProperty(OutputKeys.ENCODING, DEFAULT_ENCODE);
tf.setOutputProperties(props);
StringWriter writer = new StringWriter();
tf.transform(new DOMSource(node), new StreamResult(writer));
payload = writer.toString();
payload = payload.replaceAll(REG_INVALID_CHARS, " ");
} catch (TransformerException e) {
throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
}
return payload;
}
示例7: xsltprocess
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public void xsltprocess(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException {
// 1. Instantiate a TransformerFactory.
SAXTransformerFactory tFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
// 2. Use the TransformerFactory to process the stylesheet Source and
// generate a Transformer.
InputStream is = getClass().getResourceAsStream("xmg2pol.xsl");
Transformer transformer = tFactory.newTransformer (new StreamSource(is));
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "polarities.dtd,xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
// 3. Use the Transformer to transform an XML Source and send the
// output to a Result object.
try {
String input = args[0];
String output= args[1];
SAXSource saxs = new SAXSource(new InputSource(input));
XMLReader saxReader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
saxReader.setEntityResolver(new MyEntityResolver());
saxs.setXMLReader(saxReader);
transformer.transform(saxs, new StreamResult(new OutputStreamWriter(new FileOutputStream(output), "utf-8")));
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: MexEntityResolver
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public MexEntityResolver(List<? extends Source> wsdls) throws IOException {
Transformer transformer = XmlUtil.newTransformer();
for (Source source : wsdls) {
XMLStreamBufferResult xsbr = new XMLStreamBufferResult();
try {
transformer.transform(source, xsbr);
} catch (TransformerException e) {
throw new WebServiceException(e);
}
String systemId = source.getSystemId();
//TODO: can we do anything if the given mex Source has no systemId?
if(systemId != null){
SDDocumentSource doc = SDDocumentSource.create(JAXWSUtils.getFileOrURL(systemId), xsbr.getXMLStreamBuffer());
this.wsdls.put(systemId, doc);
}
}
}
示例9: moduleDoc
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* CLI option: print the documentation for the specified module.
* @param name
* @throws TransformerException
* @throws XPathExpressionException
* @throws UnsupportedServiceException
* @throws ServiceInstanciationException
* @throws AmbiguousAliasException
*/
@CLIOption(value="-moduleDoc", stop=true)
public final void moduleDoc(String name) throws TransformerException, XPathExpressionException, UnsupportedServiceException, AmbiguousAliasException {
Document doc = getModuleDocumentation(name);
// same ClassLoader as this class
Source xslt = new StreamSource(getClass().getResourceAsStream(noColors ? "alvisnlp-doc2txt.xslt" : "alvisnlp-doc2ansi.xslt"));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xslt);
transformer.setParameter("name", bundle.getString(DocResourceConstants.MODULE_NAME).toUpperCase(locale));
transformer.setParameter("synopsis", bundle.getString(DocResourceConstants.SYNOPSIS).toUpperCase(locale));
transformer.setParameter("description", bundle.getString(DocResourceConstants.MODULE_DESCRIPTION).toUpperCase(locale));
transformer.setParameter("parameters", bundle.getString(DocResourceConstants.MODULE_PARAMETERS).toUpperCase(locale));
Source source = new DOMSource(doc);
Result result = new StreamResult(System.out);
transformer.transform(source, result);
}
示例10: domSourceToString
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
protected String domSourceToString() throws SQLException {
try {
DOMSource source = new DOMSource(this.asDOMResult.getNode());
Transformer identity = TransformerFactory.newInstance().newTransformer();
StringWriter stringOut = new StringWriter();
Result result = new StreamResult(stringOut);
identity.transform(source, result);
return stringOut.toString();
} catch (Throwable t) {
SQLException sqlEx = SQLError.createSQLException(t.getMessage(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
sqlEx.initCause(t);
throw sqlEx;
}
}
示例11: test
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
@Test
public void test() throws TransformerException, ParserConfigurationException, SAXException, IOException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
String out = USER_DIR + "Bug4693341.out";
StreamResult result = new StreamResult(new File(out));
String in = XML_DIR + "Bug4693341.xml";
String golden = GOLDEN_DIR + "Bug4693341.xml";
File file = new File(in);
StreamSource source = new StreamSource(file);
System.out.println(source.getSystemId());
Files.copy(Paths.get(XML_DIR + "Bug4693341.dtd"),
Paths.get(USER_DIR + "Bug4693341.dtd"), REPLACE_EXISTING);
transformer.transform(source, result);
assertTrue(compareDocumentWithGold(golden, out));
}
示例12: convertXml
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Converts an xml node to a string.
*
* @param node
* @return
*/
public String convertXml(Node node) {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(node);
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
return sw.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例13: transformStringToWriter
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Transforms an XML String using a pre-compiled {@link javax.xml.transform.Transformer}. Use {@link
* #getTransformer(String xslFilePath)} to produce a reusable {@link javax.xml.transform.Transformer} for a
* given XSL stylesheet. To convert the resulting StringWriter to a String, call StringWriter.toString().
*
* @param xmlString The XML String to transform.
* @param transformer A pre-compiled {@link javax.xml.transform.Transformer} used to produce transformed
* output.
* @return A StringWriter containing the transformed content.
*/
public final static StringWriter transformStringToWriter(String xmlString, Transformer transformer) {
try {
StringWriter writer = new StringWriter();
StreamSource source = new StreamSource(new StringReader(xmlString));
transformer.transform(source, new StreamResult(writer));
return writer;
} catch (Throwable e) {
prtlnErr(e);
return new StringWriter();
}
}
示例14: writeXML
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Writes dataset to an XML file using this specified formatter
*
* @param outputFile
* @param dataset
* @throws ParserConfigurationException
* @throws TransformerException
* @throws FileNotFoundException
*/
public void writeXML(File outputFile, Processable<RecordType> dataset)
throws ParserConfigurationException, TransformerException,
FileNotFoundException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element root = createRootElement(doc);
doc.appendChild(root);
for (RecordType record : dataset.get()) {
root.appendChild(createElementFromRecord(record, doc));
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer;
transformer = tFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(root);
StreamResult result = new StreamResult(new FileOutputStream(outputFile));
transformer.transform(source, result);
}
示例15: outputFile
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
private static void outputFile() throws TransformerException
{
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}