当前位置: 首页>>代码示例>>Java>>正文


Java SAXTransformerFactory.newTransformer方法代码示例

本文整理汇总了Java中javax.xml.transform.sax.SAXTransformerFactory.newTransformer方法的典型用法代码示例。如果您正苦于以下问题:Java SAXTransformerFactory.newTransformer方法的具体用法?Java SAXTransformerFactory.newTransformer怎么用?Java SAXTransformerFactory.newTransformer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.xml.transform.sax.SAXTransformerFactory的用法示例。


在下文中一共展示了SAXTransformerFactory.newTransformer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: xsltprocess

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的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();
   	}
}
 
开发者ID:spetitjean,项目名称:TuLiPA-frames,代码行数:26,代码来源:TransformPolarity.java

示例2: test3

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
@Test
public void test3() throws Exception {
    SAXTransformerFactory sf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    Transformer t = sf.newTransformer();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder parser = dbf.newDocumentBuilder();
    Document dom = parser.parse(Bug5072946.class.getResourceAsStream("Bug5072946.xml"));

    DOMResult r = new DOMResult();

    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());

    Node n = r.getNode().getFirstChild();
    r.setNode(n);
    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());
    Assert.assertSame(r.getNode(), n);

    r.setNextSibling(r.getNode().getFirstChild());
    t.transform(new DOMSource(dom), r);
    Assert.assertNotNull(r.getNode());
    Assert.assertSame(r.getNode(), n);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:27,代码来源:Bug5072946.java

示例3: createTransformer

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
 * Creates a new identity transformer.
 */
static Transformer createTransformer(boolean disableSecureProcessing) {
    try {
        SAXTransformerFactory tf = (SAXTransformerFactory)XmlFactory.createTransformerFactory(disableSecureProcessing);
        return tf.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new Error(e); // impossible
    }
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:12,代码来源:JAXBContextImpl.java

示例4: main2

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public static void main2(String[] args) throws Exception {
        IntegratorOutputNode root = new IntegratorOutputNode();
        boolean testProblem = true;
        if (testProblem) {
            root.addDescendent("/record/marc", new InputSourceXMLReader(getXR(), new InputSource("./src/test/resources/input/test-problem/bib.xml")), false);
            root.addDescendent("/record/holdings/holding", new InputSourceXMLReader(getXR(), new InputSource("./src/test/resources/input/test-problem/mfhd.xml")), false);
            root.addDescendent("/record/holdings/holding/items/item", new InputSourceXMLReader(getXR(), new InputSource("./src/test/resources/input/test-problem/item.xml")), false);
        } else {
            root.addDescendent("/record/marc", new InputSourceXMLReader(getXR(), new InputSource("./src/test/resources/input/real/marc.xml")), false);
            root.addDescendent("/record/holdings/holding", new InputSourceXMLReader(getXR(), new InputSource("./src/test/resources/input/real/hldg.xml")), false);
            root.addDescendent("/record/holdings/holding/items/item", new InputSourceXMLReader(getXR(), new InputSource("./src/test/resources/input/real/itemAll.xml")), false);
            root.addDescendent("/record/holdings/holding/items/item/itemStatuses/itemStatus", new InputSourceXMLReader(getXR(), new InputSource("./src/test/resources/input/real/itemStatus.xml")), false);
        }
//        root.addDescendent("/items/item", new PreConfiguredXMLReader(new InputSource("./src/test/resources/input/real/item.xml")), false);
//        root.addDescendent("/items/item/itemStatuses/itemStatus", new PreConfiguredXMLReader(new InputSource("./src/test/resources/input/real/itemStatus.xml")), false);
        ExecutorService executor = Executors.newCachedThreadPool();
        root.setProperty(SAXProperties.EXECUTOR_SERVICE_PROPERTY_NAME, executor);
        SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null);
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        one(t, root, "/tmp/output.xml");
        root.reset();
        t.reset();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        one(t, root, "/tmp/output2.xml");
        executor.shutdown();
        System.err.println("DONE!");
    }
 
开发者ID:upenn-libraries,项目名称:xmlaminar,代码行数:29,代码来源:IntegratorOutputNode.java

示例5: verifyTest

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private static void verifyTest(IntegratorOutputNode root, String fileName, boolean verify) throws TransformerConfigurationException, TransformerException, IOException, ParserConfigurationException, SAXException {
    SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null);
    Transformer t = tf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    UnboundedContentHandlerBuffer out = new UnboundedContentHandlerBuffer();
    try {
        t.transform(new SAXSource(root, new InputSource()), new SAXResult(out));
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
    out.dump(System.out, false);
}
 
开发者ID:upenn-libraries,项目名称:xmlaminar,代码行数:13,代码来源:IntegratorOutputNodeTest.java

示例6: main

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public static void main(String[] args) throws TransformerConfigurationException, TransformerException, FileNotFoundException, IOException {

        RSXMLReader instance = new RSXMLReader();
        instance.setDataSource(newDataSource(new File("work/connection.properties")));
        instance.setSql(sqlItemStatus);
        instance.setIdFieldLabels(itemStatusIdFields);

        SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance(TRANSFORMER_FACTORY_CLASS_NAME, null);
        Transformer t = stf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        FileOutputStream fos = new FileOutputStream("/tmp/item_status.xml");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        t.transform(new SAXSource(instance, new InputSource()), new StreamResult(bos));
        bos.close();
    }
 
开发者ID:upenn-libraries,项目名称:xmlaminar,代码行数:16,代码来源:RSXMLReader.java

示例7: runTestInstanceToFile

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public static void runTestInstanceToFile(BinaryMARCXMLReader instance) throws TransformerConfigurationException, TransformerException, IOException {
    SAXTransformerFactory stf = (SAXTransformerFactory)TransformerFactory.newInstance(TRANSFORMER_FACTORY_CLASS_NAME, null);
    Transformer t = stf.newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    ExecutorService exec = Executors.newCachedThreadPool();
    instance.setExecutor(exec);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("/tmp/marc.xml"));
    long start = System.currentTimeMillis();
    t.transform(new SAXSource(instance, new InputSource(new StringReader("12\n7113\n7540"))), new StreamResult(bos));
    t.transform(new SAXSource(instance, new InputSource(new StringReader("7113\n7540"))), new StreamResult(bos));
    t.transform(new SAXSource(instance, new InputSource(new StringReader("7540"))), new StreamResult(bos));
    bos.close();
    System.out.println("duration: "+(System.currentTimeMillis() - start));
    exec.shutdown();
}
 
开发者ID:upenn-libraries,项目名称:xmlaminar,代码行数:16,代码来源:BinaryMARCXMLReader.java

示例8: newHandler

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
public static ContentHandler newHandler(OutputStream out, String encoding, boolean indent)
        throws TransformerConfigurationException {
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler hd = tf.newTransformerHandler();
    Transformer serializer = tf.newTransformer();
    StreamResult stream = new StreamResult(out);
    serializer.setOutputProperty(OutputKeys.ENCODING, encoding);
    serializer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
    hd.setResult(stream);
    return hd;
}
 
开发者ID:apache,项目名称:ant-ivy,代码行数:12,代码来源:OBRXMLWriter.java

示例9: showTransformer

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
private static void showTransformer(Connection con) {

      try {
          // Create and execute an SQL statement that returns a
          // set of data.       
          String SQL = "SELECT * FROM TestTable1";
          Statement stmt = con.createStatement();
          ResultSet rs = stmt.executeQuery(SQL);
	       
          rs.next();
	        
          // Get the value of the source SQLXML object from the database.
          SQLXML xmlSource = rs.getSQLXML("Col3");
	        
          // Get a Source to read the XML data. 
          SAXSource sxSource =  xmlSource.getSource(SAXSource.class);
	        
         // Create a destination SQLXML object without any data.
         SQLXML xmlDest = con.createSQLXML();

         // Get a Result to write the XML data.
         SAXResult sxResult = xmlDest.setResult(SAXResult.class);
	        
         // Transform the Source to a Result by using the identity transform.
         SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
         Transformer identity = stf.newTransformer();
         identity.transform(sxSource, sxResult);

         // Insert the destination SQLXML object into the database.
         PreparedStatement psmt = 
           con.prepareStatement(
              "INSERT INTO TestTable2" + " (Col2, Col3, Col4, Col5) VALUES (?, ?, ?, ?)");
         psmt.setString(1, "A");
         psmt.setString(2, "Test data");
         psmt.setInt(3, 123);
         psmt.setSQLXML(4, xmlDest);
         psmt.execute();
	        
         // Execute the query and display the data.	
         SQL = "SELECT * FROM TestTable2";
         stmt = con.createStatement();
         rs = stmt.executeQuery(SQL);

         System.out.println("showTransformer method : Display data in TestTable2 => ");
         while (rs.next()) {
            System.out.println(rs.getString("Col1") + " : " + rs.getString("Col2"));
            System.out.println(rs.getString("Col3") + " : " + rs.getInt("Col4"));
	          
            SQLXML xml = rs.getSQLXML("Col5");              
            System.out.println("XML column : " + xml.getString());
         }
     } catch (Exception e) {
        e.printStackTrace();
     }
   }
 
开发者ID:lfkdsk,项目名称:PracticeCode,代码行数:56,代码来源:sqlxmlExample.java

示例10: updateTranslationFile

import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
 * Updates the translation file with new entries for newly found event producer methods.
 * @param modelFile the model file to use
 * @throws IOException if an I/O error occurs
 */
protected void updateTranslationFile(File modelFile) throws IOException {
    try {
        boolean resultExists = getTranslationFile().exists();
        SAXTransformerFactory tFactory
            = (SAXTransformerFactory)SAXTransformerFactory.newInstance();

        //Generate fresh generated translation file as template
        Source src = new StreamSource(modelFile.toURI().toURL().toExternalForm());
        StreamSource xslt1 = new StreamSource(
                getClass().getResourceAsStream(MODEL2TRANSLATION));
        if (xslt1.getInputStream() == null) {
            throw new FileNotFoundException(MODEL2TRANSLATION + " not found");
        }
        DOMResult domres = new DOMResult();
        Transformer transformer = tFactory.newTransformer(xslt1);
        transformer.transform(src, domres);
        final Node generated = domres.getNode();

        Node sourceDocument;
        if (resultExists) {
            //Load existing translation file into memory (because we overwrite it later)
            src = new StreamSource(getTranslationFile().toURI().toURL().toExternalForm());
            domres = new DOMResult();
            transformer = tFactory.newTransformer();
            transformer.transform(src, domres);
            sourceDocument = domres.getNode();
        } else {
            //Simply use generated as source document
            sourceDocument = generated;
        }

        //Generate translation file (with potentially new translations)
        src = new DOMSource(sourceDocument);

        //The following triggers a bug in older Xalan versions
        //Result res = new StreamResult(getTranslationFile());
        OutputStream out = new java.io.FileOutputStream(getTranslationFile());
        out = new java.io.BufferedOutputStream(out);
        Result res = new StreamResult(out);
        try {
            StreamSource xslt2 = new StreamSource(
                    getClass().getResourceAsStream(MERGETRANSLATION));
            if (xslt2.getInputStream() == null) {
                throw new FileNotFoundException(MERGETRANSLATION + " not found");
            }
            transformer = tFactory.newTransformer(xslt2);
            transformer.setURIResolver(new URIResolver() {
                public Source resolve(String href, String base) throws TransformerException {
                    if ("my:dom".equals(href)) {
                        return new DOMSource(generated);
                    }
                    return null;
                }
            });
            if (resultExists) {
                transformer.setParameter("generated-url", "my:dom");
            }
            transformer.transform(src, res);
            if (resultExists) {
                log("Translation file updated: " + getTranslationFile());
            } else {
                log("Translation file generated: " + getTranslationFile());
            }
        } finally {
            IOUtils.closeQuietly(out);
        }
    } catch (TransformerException te) {
        throw new IOException(te.getMessage());
    }
}
 
开发者ID:pellcorp,项目名称:fop,代码行数:76,代码来源:EventProducerCollectorTask.java


注:本文中的javax.xml.transform.sax.SAXTransformerFactory.newTransformer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。