简体   繁体   中英

using javax.xml.transform.Transformer

I'm not understanding what happens to the StreamResult instance. I see that the Transformer object receives source and streamResult :

    transformer.transform(source, streamResult);

this transforms source into streamResult ? It seems odd that there's nothing returned from this operation yet streamResult now has, for lack of a better word, data. Is it xml data?

package helloWorldSaxon;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;
import java.util.logging.Logger;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class App {

    private static final Logger LOG = Logger.getLogger(App.class.getName());
    private final Properties properties = new Properties();

    public static void main(String[] args) throws TransformerException, TransformerConfigurationException, IOException, SAXException {
        LOG.fine("starting..");
        new App().identyTransformOnURL();
    }

    private void identyTransformOnURL() throws TransformerConfigurationException, TransformerException, IOException, SAXException {
        properties.loadFromXML(App.class.getResourceAsStream("/saxon.xml"));
        String url = properties.getProperty("url");

        StringWriter writer = new StringWriter();
        StreamResult streamResult = new StreamResult(writer);

        TransformerFactory factory = TransformerFactory.newInstance();
        XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");
        Source source = new SAXSource(xmlReader, new InputSource(url));

        Transformer transformer = factory.newTransformer();
        transformer.transform(source, streamResult);

        String stringResult = writer.toString();
        LOG.info(stringResult);
    }

}

I don't understand the JavaDoc on this method:

Transform the XML Source to a Result. 

Wouldn't it be more intuitive to return a result?

The line in question takes in an XML source and another object (ie Result ) where the results of the transformation can be written. In essence, the second argument is a bucket you provide that allows the transform method to place its results. Once the method call is over, you retrieve your bucket and take the results out of it.

In your particular case:

transformer.transform(source, streamResult);

The streamResult object is a streamed writer that encapsulates a StringWriter . This streamResult object is then passed to the transform method, which writes its results to the StreamResult , which in turn writes the results to the StringWriter . You can then call the toString method on the StringWriter to view the stringified results of the transformation.

To answer your second question: Some methods do not return their results as a matter of efficiency and flexibility. For example, suppose a very large number of transformations are performed. If the results were returned, a new object would need to be created for each one of these transformations. In the case where a result object is passed in (called an out parameter ), the same result object can be reused over and over. Secondly, returning the result removes some flexibility, as it does not allow the user to specify how the results should be written. In your case, you were able to pass in a StringWriter wrapped inside a StreamResult , but you just as easily could have passed in any object that implements the Result interface.

There are some alternatives to this type of out parameter using lambda expressions and functional programming, but at the time XML transformation interface was written, out parameters were a common practice (and in some cases, still are).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM