XMLResource
提供了一個getContentAsSAX
method將XML直接提供給SAX ContentHandler。而javax.xml.transform提供了TransformerHandler
的概念,將Transformer公開爲SAX ContentHandler。所以,你可以使用這兩個組合:
Source stylesheet = ... // however you're loading your stylesheet
TransformerHandler handler = ((SAXTransformerFactory)transformerFactory)
.newTransformerHandler(stylesheet);
Result result = ... // StreamResult, DOMResult etc.
handler.setResult(result);
xmlResource.getContentAsSAX(handler);
// result now holds the result of the transformation
對於相反的方向,如果你需要一個XMLResource
充當目標轉型的,那麼你可以使用SAXResult
與正常Transformer
Source stylesheet = ... // however you're loading your stylesheet
Transformer transformer = transformerFactory.newTransformer(stylesheet);
Source source = ... // StreamSource, DOMSource, etc.
Result result = new SAXResult(xmlResource.setContentAsSAX());
transformer.transform(source, result);