當前位置: 首頁>>代碼示例>>Java>>正文


Java StreamResult類代碼示例

本文整理匯總了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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:Bug7037352Test.java

示例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();
	}
}
 
開發者ID:bsteker,項目名稱:bdf2,代碼行數:17,代碼來源:GraphicalEditorPage.java

示例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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:SAXTFactoryTest.java

示例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)));
}
 
開發者ID:phweda,項目名稱:MFM,代碼行數:14,代碼來源:PersistUtils.java

示例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());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:Bug6540545.java

示例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 [] {};
}
 
開發者ID:WasiqB,項目名稱:coteafs-services,代碼行數:21,代碼來源:XmlPayloadLogger.java

示例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);
  }
}
 
開發者ID:gurkenlabs,項目名稱:litiengine,代碼行數:26,代碼來源:XmlUtilities.java

示例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;
    }
}
 
開發者ID:Jugendhackt,項目名稱:OpenVertretung,代碼行數:17,代碼來源:JDBC4MysqlSQLXML.java

示例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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:AuctionItemRepository.java

示例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);
    }
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:24,代碼來源:XMLUtil.java

示例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));
}
 
開發者ID:beesenpai,項目名稱:EVE,代碼行數:20,代碼來源:Weather.java

示例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));
    }
}
 
開發者ID:goldmansachs,項目名稱:tablasco,代碼行數:17,代碼來源:ExceptionHtml.java

示例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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:SAXTFactoryTest.java

示例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;
}
 
開發者ID:langxianwei,項目名稱:iot-plat,代碼行數:18,代碼來源:ConfigManager.java

示例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);
	}
}
 
開發者ID:hivdb,項目名稱:sierra,代碼行數:17,代碼來源:XmlOutput.java


注:本文中的javax.xml.transform.stream.StreamResult類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。