XMLResource
提供了getContentAsSAX
一种将 XML 直接提供给 SAX ContentHandler 的方法。并且 javax.xml.transform 提供了 a 的概念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);