Designated signature
<yourxml>
...
<Signature>....</Signature>
</yourxml>
A signature is a node of an XML document. After verifying the XML signature, find the node, remove it from the DOM structure and save the document.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().parse(new FileInputStream(xml));
NodeList nl = doc.getElementsByTagNameNS(XMLSignature.XMLNS, "Signature");
nl.item(0).getParentNode().removeChild(nl.item(0));
OutputStream os = new FileOutputStream(outputFileName);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));
Envelope signature
<Signature>
<Object Id="object">
<yourxml>...</yourxml>
</Object>
</Signature>
You can apply the same technique. Find Objectnode and save the first file in a file. But in this case, XMLSignatureprovides a method getObjectsfor getting signed objects
XMLSignature signature = ...
XMLObject xmlObject = (XMLObject)signature.getObjects().get(0);
Node yourXmlNode = ((DOMStructure)xmlObject.getContent().get(0)).getNode();
OutputStream os = new FileOutputStream(outputFileName);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(yourXmlNode), new StreamResult(os));
source
share