本文整理匯總了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();
}
}
示例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);
}
示例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
}
}
示例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!");
}
示例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);
}
示例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();
}
示例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();
}
示例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;
}
示例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();
}
}
示例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());
}
}