background image

Printing the Resulting Document

<< Assembling the XML Signature | 8. Securing Web Services >>
<< Assembling the XML Signature | 8. Securing Web Services >>
230
J
AVA
XML D
IGITAL
S
IGNATURE
API
Notice that we haven't actually generated the signature yet; we'll do that in the
next step.
Generating the XML Signature
Now we are ready to generate the signature, which we do by invoking the
sign
method on the
XMLSignature
object, and pass it the signing context as follows:
signature.sign(dsc);
The resulting document now contains a signature, which has been inserted as the
last child element of the root element.
Printing or Displaying the Resulting Document
You can use the following code to print the resulting signed document to a file or
standard output:
OutputStream os;
if (args.length > 1) {
os = new FileOutputStream(args[1]);
} else {
os = System.out;
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));