Plugging in the Transformer

To transform a dom4jb generated DOM or XPath tree by means of XSLT, you can use the transformation API for XML (TrAX) . The steps needed to plug such a dom4jb generated tree into the transformer are almost the same, as plugging in any other javax.xml.transform.Source object.

In detail, you have to do the following steps:

  1. Instantiate a TransformerFactory.
  2. Use the TransformerFactory to process the stylesheet Source and generate a Transformer.
  3. Instantiate a dom4jb com.teamkonzept.dom4jb.dom.DOMSource object.
  4. Use the Transformer to transform an XML Source and send the output to a Result object.

For example, these steps could be done as follows:

import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import com.teamkonzept.dom4jb.dom.Document;
import com.teamkonzept.dom4jb.dom.Element;

    ...

    // instantiate the root of the object, which should be
    // transformed
    Address address = new Address("Michael Mueller",
                                    "Berliner Str. 12", "Berlin");
                                    
    // instantiate transformer
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    
    // instantiate dom4jb DOM document
    Document document = new Document();
    
    // instantiate root element and plug it into the document
    Element root = document.createElement("address", address);
    document.appendChild(root);
            
    // instantiate source object
    Source src = new DOMSource(document);
    
    // instantiate result object
    Result result = new StreamResult(writer);
    
    // do transformation
    transformer.transform(src, result);    

                

If you prefer to use the XPath implementation for the saxon processor - which is currently the more efficient way. Then the only thing you have to do is, use a com.teamkonzept.dom4jb.saxon.Document object instead of the com.teamkonzept.dom4jb.dom.Document object, w.r.t. the example above.