0

w3c.dom.Document can be used as a Node, but when I try this

public static Object tranform(Source source) {
        Object result = null;
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer newTransformer = tf.newTransformer();
            newTransformer.transform(source, (Result) result);
        } catch (TransformerException ex) {
            Logger.getLogger(XMLUtils.class.getName()).log(Level.SEVERE, null, ex);
        } 
        return result;
    }

on the other file

     Document doc = (Document) XMLUtils.tranform(source);
     // pass this doc to a function that that a Node typed parameter
     if (node == null) {
         return;
     } else {
         ...
     }

node would always be null, and it has no child node, too. I tried to convert every type of source to every type of result, coders will cast the returned result to the correct type they need. So could you please 1) Explain my error? 2) Give me a hint how to implement that purpose?

Thank you and best regards

4

1 回答 1

1

I think you need to create a class implementing Result for instance a DOMResult:

Result result = new DOMResult();

newTransformer.transform(source, result);

return result.getNode();
于 2013-07-14T17:21:37.533 回答