本文整理汇总了Java中javax.xml.transform.stream.StreamResult类的典型用法代码示例。如果您正苦于以下问题:Java StreamResult类的具体用法?Java StreamResult怎么用?Java StreamResult使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StreamResult类属于javax.xml.transform.stream包,在下文中一共展示了StreamResult类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
@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());
}
}
示例2: synchroGraphicalToXml
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
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();
}
}
示例3: testcase09
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
/**
* 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));
}
示例4: saveXMLDoctoFile
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
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)));
}
示例5: test
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
@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());
}
}
示例6: getPayload
import javax.xml.transform.stream.StreamResult; //导入依赖的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 [] {};
}
示例7: saveWithCustomIndetation
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
/**
* 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);
}
}
示例8: domSourceToString
import javax.xml.transform.stream.StreamResult; //导入依赖的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;
}
}
示例9: testXIncludeFallbackDOMPos
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
/**
* 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));
}
示例10: write
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
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);
}
}
示例11: generateWeather
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
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));
}
示例12: writeDocument
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
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));
}
}
示例13: testcase02
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
/**
* 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));
}
示例14: saveFile
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
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;
}
示例15: toString
import javax.xml.transform.stream.StreamResult; //导入依赖的package包/类
@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);
}
}