📄 examples.java
字号:
xslSource.setSystemId(xslID);
// Create a transformer for the stylesheet.
Transformer transformer = tfactory.newTransformer(xslSource);
// Note that in this case the XML encoding can not be processed!
Reader xmlReader = new BufferedReader(new InputStreamReader(new FileInputStream(sourceID), "UTF-8"));
StreamSource xmlSource = new StreamSource(xmlReader);
// Note that if we don't do this, relative URLs can not be resolved correctly!
xmlSource.setSystemId(sourceID);
// Transform the source XML to System.out.
transformer.transform( xmlSource, new StreamResult(new OutputStreamWriter(System.out)));
}
/**
* Show the simplest possible transformation from system id to output stream.
*/
public static void exampleUseTemplatesObj(String sourceID1,
String sourceID2,
String xslID)
throws TransformerException, TransformerConfigurationException
{
TransformerFactory tfactory = TransformerFactory.newInstance();
// Create a templates object, which is the processed,
// thread-safe representation of the stylesheet.
Templates templates = tfactory.newTemplates(new StreamSource(xslID));
// Illustrate the fact that you can make multiple transformers
// from the same template.
Transformer transformer1 = templates.newTransformer();
Transformer transformer2 = templates.newTransformer();
System.out.println("\n\n----- transform of "+sourceID1+" -----");
transformer1.transform(new StreamSource(sourceID1),
new StreamResult(new OutputStreamWriter(System.out)));
System.out.println("\n\n----- transform of "+sourceID2+" -----");
transformer2.transform(new StreamSource(sourceID2),
new StreamResult(new OutputStreamWriter(System.out)));
}
/**
* Show the Transformer using SAX events in and SAX events out.
*/
public static void exampleContentHandlerToContentHandler(String sourceID,
String xslID)
throws TransformerException,
TransformerConfigurationException,
SAXException, IOException
{
TransformerFactory tfactory = TransformerFactory.newInstance();
// Does this factory support SAX features?
if (tfactory.getFeature(SAXSource.FEATURE))
{
// If so, we can safely cast.
SAXTransformerFactory stfactory = ((SAXTransformerFactory) tfactory);
// A TransformerHandler is a ContentHandler that will listen for
// SAX events, and transform them to the result.
TransformerHandler handler
= stfactory.newTransformerHandler(new StreamSource(xslID));
// Set the result handling to be a serialization to System.out.
Result result = new SAXResult(new ExampleContentHandler());
handler.setResult(result);
// Create a reader, and set it's content handler to be the TransformerHandler.
XMLReader reader=null;
// Use JAXP1.1 ( if possible )
try {
javax.xml.parsers.SAXParserFactory factory=
javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware( true );
javax.xml.parsers.SAXParser jaxpParser=
factory.newSAXParser();
reader=jaxpParser.getXMLReader();
} catch( javax.xml.parsers.ParserConfigurationException ex ) {
throw new org.xml.sax.SAXException( ex );
} catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
throw new org.xml.sax.SAXException( ex1.toString() );
} catch( NoSuchMethodError ex2 ) {
}
if( reader==null ) reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(handler);
// It's a good idea for the parser to send lexical events.
// The TransformerHandler is also a LexicalHandler.
reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
// Parse the source XML, and send the parse events to the TransformerHandler.
reader.parse(sourceID);
}
else
{
System.out.println(
"Can't do exampleContentHandlerToContentHandler because tfactory is not a SAXTransformerFactory");
}
}
/**
* Show the Transformer as a SAX2 XMLReader. An XMLFilter obtained
* from newXMLFilter should act as a transforming XMLReader if setParent is not
* called. Internally, an XMLReader is created as the parent for the XMLFilter.
*/
public static void exampleXMLReader(String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException, SAXException, IOException // , ParserConfigurationException
{
TransformerFactory tfactory = TransformerFactory.newInstance();
if(tfactory.getFeature(SAXSource.FEATURE))
{
XMLReader reader
= ((SAXTransformerFactory) tfactory).newXMLFilter(new StreamSource(xslID));
reader.setContentHandler(new ExampleContentHandler());
reader.parse(new InputSource(sourceID));
}
else
System.out.println("tfactory does not support SAX features!");
}
/**
* Show the Transformer as a simple XMLFilter. This is pretty similar
* to exampleXMLReader, except that here the parent XMLReader is created
* by the caller, instead of automatically within the XMLFilter. This
* gives the caller more direct control over the parent reader.
*/
public static void exampleXMLFilter(String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException, SAXException, IOException // , ParserConfigurationException
{
TransformerFactory tfactory = TransformerFactory.newInstance();
XMLReader reader=null;
// Use JAXP1.1 ( if possible )
try {
javax.xml.parsers.SAXParserFactory factory=
javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware( true );
javax.xml.parsers.SAXParser jaxpParser=
factory.newSAXParser();
reader=jaxpParser.getXMLReader();
} catch( javax.xml.parsers.ParserConfigurationException ex ) {
throw new org.xml.sax.SAXException( ex );
} catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
throw new org.xml.sax.SAXException( ex1.toString() );
} catch( NoSuchMethodError ex2 ) {
}
if( reader==null ) reader = XMLReaderFactory.createXMLReader();
// The transformer will use a SAX parser as it's reader.
reader.setContentHandler(new ExampleContentHandler());
try
{
reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
true);
reader.setFeature("http://apache.org/xml/features/validation/dynamic",
true);
}
catch (SAXException se)
{
// What can we do?
// TODO: User diagnostics.
}
XMLFilter filter
= ((SAXTransformerFactory) tfactory).newXMLFilter(new StreamSource(xslID));
filter.setParent(reader);
// Now, when you call transformer.parse, it will set itself as
// the content handler for the parser object (it's "parent"), and
// will then call the parse method on the parser.
filter.parse(new InputSource(sourceID));
}
/**
* This example shows how to chain events from one Transformer
* to another transformer, using the Transformer as a
* SAX2 XMLFilter/XMLReader.
*/
public static void exampleXMLFilterChain(
String sourceID, String xslID_1,
String xslID_2, String xslID_3)
throws TransformerException, TransformerConfigurationException, SAXException, IOException
{
TransformerFactory tfactory = TransformerFactory.newInstance();
Templates stylesheet1 = tfactory.newTemplates(new StreamSource(xslID_1));
Transformer transformer1 = stylesheet1.newTransformer();
// If one success, assume all will succeed.
if (tfactory.getFeature(SAXSource.FEATURE))
{
SAXTransformerFactory stf = (SAXTransformerFactory)tfactory;
XMLReader reader=null;
// Use JAXP1.1 ( if possible )
try {
javax.xml.parsers.SAXParserFactory factory=
javax.xml.parsers.SAXParserFactory.newInstance();
factory.setNamespaceAware( true );
javax.xml.parsers.SAXParser jaxpParser=
factory.newSAXParser();
reader=jaxpParser.getXMLReader();
} catch( javax.xml.parsers.ParserConfigurationException ex ) {
throw new org.xml.sax.SAXException( ex );
} catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
throw new org.xml.sax.SAXException( ex1.toString() );
} catch( NoSuchMethodError ex2 ) {
}
if( reader==null ) reader = XMLReaderFactory.createXMLReader();
XMLFilter filter1 = stf.newXMLFilter(new StreamSource(xslID_1));
XMLFilter filter2 = stf.newXMLFilter(new StreamSource(xslID_2));
XMLFilter filter3 = stf.newXMLFilter(new StreamSource(xslID_3));
if (null != filter1) // If one success, assume all were success.
{
// transformer1 will use a SAX parser as it's reader.
filter1.setParent(reader);
// transformer2 will use transformer1 as it's reader.
filter2.setParent(filter1);
// transform3 will use transform2 as it's reader.
filter3.setParent(filter2);
filter3.setContentHandler(new ExampleContentHandler());
// filter3.setContentHandler(new org.xml.sax.helpers.DefaultHandler());
// Now, when you call transformer3 to parse, it will set
// itself as the ContentHandler for transform2, and
// call transform2.parse, which will set itself as the
// content handler for transform1, and call transform1.parse,
// which will set itself as the content listener for the
// SAX parser, and call parser.parse(new InputSource("xml/foo.xml")).
filter3.parse(new InputSource(sourceID));
}
else
{
System.out.println(
"Can't do exampleXMLFilter because "+
"tfactory doesn't support asXMLFilter()");
}
}
else
{
System.out.println(
"Can't do exampleXMLFilter because "+
"tfactory is not a SAXTransformerFactory");
}
}
/**
* Show how to transform a DOM tree into another DOM tree.
* This uses the javax.xml.parsers to parse an XML file into a
* DOM, and create an output DOM.
*/
public static Node exampleDOM2DOM(String sourceID, String xslID)
throws TransformerException, TransformerConfigurationException, SAXException, IOException,
ParserConfigurationException
{
TransformerFactory tfactory = TransformerFactory.newInstance();
if (tfactory.getFeature(DOMSource.FEATURE))
{
Templates templates;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -