extractAttachments() is intended to extract base64-encoded bulky data chunks from DXL (and replace with a reference to a TMP file), before the DXL is passed to XSLT transformer – otherwise it causes XSLT transformer to consume a lot of memory and to lose performance.
extractAttachments() deals with DXL not as with XML, but as with a long string. So it is sensitive to some XML formatting. The version I posted searches/replaces attachments more-less stable. You may use PdfAgent(R7).java or Dxl2Pdf.java extractAttachments() as a usage template (I thought your code is derived from one of the Java classes).
PD4ML may deal equally with attachments originally embedded in DXL or with references to extracted attachments. With the references it is asa a rule more performant.