/**
* Save a single emitter to the XML file
*
* @param out
* The location to which we should save
* @param emitter
* The emitter to store to the XML file
* @throws IOException
* Indicates a failure to write or encode the XML
*/
public static void saveEmitter(OutputStream out, ConfigurableEmitter emitter)
throws IOException {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = builder.newDocument();
document.appendChild(emitterToElement(document, emitter));
Result result = new StreamResult(new OutputStreamWriter(out,
"utf-8"));
DOMSource source = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer xformer = factory.newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
} catch (Exception e) {
Log.error(e);
throw new IOException("Failed to save emitter");
}
}
java类javax.xml.transform.stream.StreamResult的实例源码
ParticleIO.java 文件源码
项目:trashjam2017
阅读 30
收藏 0
点赞 0
评论 0
GraphicalEditorPage.java 文件源码
项目:bdf2
阅读 41
收藏 0
点赞 0
评论 0
public void synchroGraphicalToXml(){
Document doc=this.buildDocument();
if(doc==null)return;
TransformerFactory factory=TransformerFactory.newInstance();
try{
Transformer transformer=factory.newTransformer();
transformer.setOutputProperty("encoding","utf-8");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc),new StreamResult(out));
xmlEditor.getDocumentProvider().getDocument(xmlEditor.getEditorInput()).set(out.toString("utf-8"));
out.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
SAXTFactoryTest.java 文件源码
项目:openjdk-jdk10
阅读 22
收藏 0
点赞 0
评论 0
/**
* Test newTransformerHandler with a Template Handler along with a relative
* URI in the style-sheet file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase09() throws Exception {
String outputFile = USER_DIR + "saxtf009.out";
String goldFile = GOLDEN_DIR + "saxtf009GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory)TransformerFactory.newInstance();
TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
thandler.setSystemId("file:///" + XML_DIR);
reader.setContentHandler(thandler);
reader.parse(XSLT_INCL_FILE);
TransformerHandler tfhandler=
saxTFactory.newTransformerHandler(thandler.getTemplates());
Result result = new StreamResult(fos);
tfhandler.setResult(result);
reader.setContentHandler(tfhandler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
PersistUtils.java 文件源码
项目:MFM
阅读 34
收藏 0
点赞 0
评论 0
public static void saveXMLDoctoFile(Document doc, DocumentType documentType, String path)
throws TransformerException {
// Save DOM XML doc to File
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
doc.setXmlVersion("1.0");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId());
transformer.transform(new DOMSource(doc), new StreamResult(new File(path)));
}
Bug6540545.java 文件源码
项目:openjdk-jdk10
阅读 36
收藏 0
点赞 0
评论 0
@Test
public void test() {
try {
String xmlFile = "numbering63.xml";
String xslFile = "numbering63.xsl";
TransformerFactory tFactory = TransformerFactory.newInstance();
// tFactory.setAttribute("generate-translet", Boolean.TRUE);
Transformer t = tFactory.newTransformer(new StreamSource(getClass().getResourceAsStream(xslFile), getClass().getResource(xslFile).toString()));
StringWriter sw = new StringWriter();
t.transform(new StreamSource(getClass().getResourceAsStream(xmlFile)), new StreamResult(sw));
String s = sw.getBuffer().toString();
Assert.assertFalse(s.contains("1: Level A"));
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
XmlPayloadLogger.java 文件源码
项目:coteafs-services
阅读 23
收藏 0
点赞 0
评论 0
@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 [] {};
}
XmlUtilities.java 文件源码
项目:litiengine
阅读 29
收藏 0
点赞 0
评论 0
/**
* Saves the xml, contained by the specified input with the custom indentation.
* If the input is the result of jaxb marshalling, make sure to set
* Marshaller.JAXB_FORMATTED_OUTPUT to false in order for this method to work
* properly.
*
* @param input
* @param fos
* @param indentation
*/
public static void saveWithCustomIndetation(ByteArrayInputStream input, FileOutputStream fos, int indentation) {
try {
Transformer transformer = SAXTransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentation));
Source xmlSource = new SAXSource(new org.xml.sax.InputSource(input));
StreamResult res = new StreamResult(fos);
transformer.transform(xmlSource, res);
fos.flush();
fos.close();
} catch (TransformerFactoryConfigurationError | TransformerException | IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
JDBC4MysqlSQLXML.java 文件源码
项目:OpenVertretung
阅读 38
收藏 0
点赞 0
评论 0
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;
}
}
AuctionItemRepository.java 文件源码
项目:openjdk-jdk10
阅读 33
收藏 0
点赞 0
评论 0
/**
* Test the simple case of including a document using xi:include within a
* xi:fallback using a DocumentBuilder.
*
* @throws Exception If any errors occur.
*/
@Test(groups = {"readWriteLocalFiles"})
public void testXIncludeFallbackDOMPos() throws Exception {
String resultFile = USER_DIR + "doc_fallbackDOM.out";
String goldFile = GOLDEN_DIR + "doc_fallbackGold.xml";
String xmlFile = XML_DIR + "doc_fallback.xml";
try (FileOutputStream fos = new FileOutputStream(resultFile)) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setXIncludeAware(true);
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(new File(xmlFile));
doc.setXmlStandalone(true);
TransformerFactory.newInstance().newTransformer()
.transform(new DOMSource(doc), new StreamResult(fos));
}
assertTrue(compareDocumentWithGold(goldFile, resultFile));
}
XMLUtil.java 文件源码
项目:incubator-netbeans
阅读 36
收藏 0
点赞 0
评论 0
public static void write(Document doc, OutputStream out) throws IOException {
// XXX note that this may fail to write out namespaces correctly if the document
// is created with namespaces and no explicit prefixes; however no code in
// this package is likely to be doing so
try {
Transformer t = TransformerFactory.newInstance().newTransformer(
new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
DocumentType dt = doc.getDoctype();
if (dt != null) {
String pub = dt.getPublicId();
if (pub != null) {
t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
}
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
}
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
Source source = new DOMSource(doc);
Result result = new StreamResult(out);
t.transform(source, result);
} catch (Exception | TransformerFactoryConfigurationError e) {
throw new IOException(e);
}
}
Weather.java 文件源码
项目:EVE
阅读 39
收藏 0
点赞 0
评论 0
private void generateWeather(String c) throws IOException, SAXException, TransformerException, ParserConfigurationException {
city = c;
// creating the URL
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&mode=xml&appid=" + APIKey;
// printing out XML
URL urlString = new URL(url);
URLConnection conn = urlString.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
TransformerFactory transformer = TransformerFactory.newInstance();
Transformer xform = transformer.newTransformer();
xform.transform(new DOMSource(doc), new StreamResult(System.out));
}
ExceptionHtml.java 文件源码
项目:tablasco
阅读 31
收藏 0
点赞 0
评论 0
private static void writeDocument(Document document, File resultsFile) throws TransformerException, IOException
{
File parentDir = resultsFile.getParentFile();
if (!parentDir.exists() && !parentDir.mkdirs())
{
throw new IllegalStateException("Unable to create results directory:" + parentDir);
}
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.METHOD, "xml");
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(resultsFile), "UTF-8")))
{
trans.transform(new DOMSource(document), new StreamResult(writer));
}
}
SAXTFactoryTest.java 文件源码
项目:openjdk-jdk10
阅读 27
收藏 0
点赞 0
评论 0
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a content handler which write out the result
* to output file. Test verifies output file is same as golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase02() throws Exception {
String outputFile = USER_DIR + "saxtf002.out";
String goldFile = GOLDEN_DIR + "saxtf002GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile);
FileInputStream fis = new FileInputStream(XSLT_FILE)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
SAXSource ss = new SAXSource();
ss.setInputSource(new InputSource(fis));
TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
ConfigManager.java 文件源码
项目:iot-plat
阅读 45
收藏 0
点赞 0
评论 0
public static boolean saveFile(Document document, File file) {
boolean flag = true;
try {
/** 将document中的内容写入文件中 */
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
/** 编码 */
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
} catch (Exception ex) {
flag = false;
ex.printStackTrace();
}
return flag;
}
Bug7037352Test.java 文件源码
项目:openjdk-jdk10
阅读 37
收藏 0
点赞 0
评论 0
@Test
public void test() {
try {
XMLOutputFactory xof = XMLOutputFactory.newInstance();
StreamResult sr = new StreamResult();
XMLStreamWriter xsw = xof.createXMLStreamWriter(sr);
NamespaceContext nc = xsw.getNamespaceContext();
System.out.println(nc.getPrefix(XMLConstants.XML_NS_URI));
System.out.println(" expected result: " + XMLConstants.XML_NS_PREFIX);
System.out.println(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
System.out.println(" expected result: " + XMLConstants.XMLNS_ATTRIBUTE);
Assert.assertTrue(nc.getPrefix(XMLConstants.XML_NS_URI) == XMLConstants.XML_NS_PREFIX);
Assert.assertTrue(nc.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI) == XMLConstants.XMLNS_ATTRIBUTE);
} catch (Throwable ex) {
Assert.fail(ex.toString());
}
}
XmlOutput.java 文件源码
项目:sierra
阅读 24
收藏 0
点赞 0
评论 0
@Override
public String toString() {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
return result.getWriter().toString();
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
XmlUtils.java 文件源码
项目:bohemia
阅读 38
收藏 0
点赞 0
评论 0
/**
* Transforms the XML content to XHTML/HTML format string with the XSL.
*
* @param payload the XML payload to convert
* @param xsltFile the XML stylesheet file
* @return the transformed XHTML/HTML format string
* @throws XmlException problem converting XML to HTML
*/
public static String xmlToHtml(String payload, File xsltFile)
throws XmlException {
String result = null;
try {
Source template = new StreamSource(xsltFile);
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(template);
Properties props = transformer.getOutputProperties();
props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
transformer.setOutputProperties(props);
StreamSource source = new StreamSource(new StringReader(payload));
StreamResult sr = new StreamResult(new StringWriter());
transformer.transform(source, sr);
result = sr.getWriter().toString();
} catch (TransformerException e) {
throw new XmlException(XmlException.XML_TRANSFORM_ERROR, e);
}
return result;
}
Bug6565260.java 文件源码
项目:openjdk-jdk10
阅读 29
收藏 0
点赞 0
评论 0
@Test
public void test() {
try {
String xmlFile = "attribset27.xml";
String xslFile = "attribset27.xsl";
TransformerFactory tFactory = TransformerFactory.newInstance();
// tFactory.setAttribute("generate-translet", Boolean.TRUE);
Transformer t = tFactory.newTransformer(new StreamSource(getClass().getResourceAsStream(xslFile)));
StringWriter sw = new StringWriter();
t.transform(new StreamSource(getClass().getResourceAsStream(xmlFile)), new StreamResult(sw));
String s = sw.getBuffer().toString();
Assert.assertFalse(s.contains("color") || s.contains("font-size"));
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
CharacterHandler.java 文件源码
项目:Aurora
阅读 39
收藏 0
点赞 0
评论 0
/**
* xml 格式化
*
* @param xml
* @return
*/
public static String xmlFormat(String xml) {
if (TextUtils.isEmpty(xml)) {
return "Empty/Null xml content";
}
String message;
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);
message = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (TransformerException e) {
message = xml;
}
return message;
}
SparqlExecutor.java 文件源码
项目:TextHIN
阅读 39
收藏 0
点赞 0
评论 0
public static void printDocument(Node node, OutputStream out) {
try {
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(node),
new StreamResult(new OutputStreamWriter(out, "UTF-8")));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
MergerXmlUtils.java 文件源码
项目:javaide
阅读 36
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
XmlUtils.java 文件源码
项目:karate
阅读 32
收藏 0
点赞 0
评论 0
public static String toString(Node node, boolean pretty) {
if (pretty) {
trimWhiteSpace(node);
}
DOMSource domSource = new DOMSource(node);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
try {
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
if (pretty) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
transformer.transform(domSource, result);
return writer.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
HtmlFormatter.java 文件源码
项目:tablasco
阅读 33
收藏 0
点赞 0
评论 0
public void appendResults(String testName, Map<String, ? extends FormattableTable> results, Metadata metadata, int verifyCount, Document dom, OutputStream outputStream) throws TransformerException, UnsupportedEncodingException
{
if (dom == null)
{
dom = createNewDocument(metadata);
}
Node body = dom.getElementsByTagName("body").item(0);
if (verifyCount == 1)
{
body.appendChild(ResultCell.createNodeWithText(dom, "h1", testName));
}
if (this.htmlOptions.isDisplayAssertionSummary())
{
appendAssertionSummary(testName, results, body);
}
for (Map.Entry<String, ? extends FormattableTable> namedTable : results.entrySet())
{
appendResults(testName, namedTable.getKey(), namedTable.getValue(), body, true);
}
TRANSFORMER.value().transform(new DOMSource(dom), new StreamResult(new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"))));
}
UpdateWarTask.java 文件源码
项目:lams
阅读 25
收藏 0
点赞 0
评论 0
/**
* Writes the modified web.xml back out to war file
*
* @param doc
* The application.xml DOM Document
* @throws org.apache.tools.ant.DeployException
* in case of any problems
*/
protected void writeWebXml(final Document doc, final OutputStream outputStream) throws DeployException {
try {
doc.normalize();
// Prepare the DOM document for writing
DOMSource source = new DOMSource(doc);
// Prepare the output file
StreamResult result = new StreamResult(outputStream);
// Write the DOM document to the file
// Get Transformer
Transformer xformer = TransformerFactory.newInstance().newTransformer();
// Write to a file
xformer.transform(source, result);
} catch (TransformerException tex) {
throw new DeployException("Error writing out modified web xml ", tex);
}
}
JDBC4MysqlSQLXML.java 文件源码
项目:lams
阅读 36
收藏 0
点赞 0
评论 0
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;
}
}
SAXTFactoryTest.java 文件源码
项目:openjdk-jdk10
阅读 41
收藏 0
点赞 0
评论 0
/**
* SAXTFactory.newTransformerhandler() method which takes SAXSource as
* argument can be set to XMLReader. SAXSource has input XML file as its
* input source. XMLReader has a transformer handler which write out the
* result to output file. Test verifies output file is same as golden file.
*
* @throws Exception If any errors occur.
*/
@Test
public void testcase01() throws Exception {
String outputFile = USER_DIR + "saxtf001.out";
String goldFile = GOLDEN_DIR + "saxtf001GF.out";
try (FileOutputStream fos = new FileOutputStream(outputFile)) {
XMLReader reader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTFactory
= (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler handler = saxTFactory.newTransformerHandler(new StreamSource(XSLT_FILE));
Result result = new StreamResult(fos);
handler.setResult(result);
reader.setContentHandler(handler);
reader.parse(XML_FILE);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
AbstractXSLRendererComponent.java 文件源码
项目:parabuild-ci
阅读 28
收藏 0
点赞 0
评论 0
/**
* Oveloaded CustomComponent's render
*/
public final void render(final RenderContext ctx) {
StreamSource xslSource = null;
StreamSource xmlSource = null;
try {
xslSource = xslSource();
xmlSource = xmlSource();
final Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
final PrintWriter pw = ctx.getWriter();
transformer.transform(xmlSource, new StreamResult(pw));
} catch (Exception e) {
showUnexpectedErrorMsg(ctx, e);
} finally {
closeHard(xslSource);
closeHard(xmlSource);
}
}
SPLOpltionGenerator.java 文件源码
项目:KeYExperienceReport
阅读 34
收藏 0
点赞 0
评论 0
private static void writeOut(Document doc) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
DOMSource source = new DOMSource(doc);
File f = new File("splFile.xml");
f.delete();
StreamResult result = new StreamResult(f);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("File saved!");
}
XmlUtilities.java 文件源码
项目:ats-framework
阅读 33
收藏 0
点赞 0
评论 0
/**
* Pretty print XML Node
* @param node
* @return
* @throws XmlUtilitiesException
*/
public String xmlNodeToString( Node node ) throws XmlUtilitiesException {
StringWriter sw = new StringWriter();
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_INDENT_AMOUNT, "4");
transformer.setOutputProperty(OutputPropertiesFactory.S_KEY_LINE_SEPARATOR, "\n");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException te) {
throw new XmlUtilitiesException("Error transforming XML node to String", te);
}
return sw.toString().trim();
}
CharacterHandler.java 文件源码
项目:MoligyMvpArms
阅读 31
收藏 0
点赞 0
评论 0
/**
* xml 格式化
*
* @param xml
* @return
*/
public static String xmlFormat(String xml) {
if (TextUtils.isEmpty(xml)) {
return "Empty/Null xml content";
}
String message;
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);
message = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (TransformerException e) {
message = xml;
}
return message;
}