Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Use the Base64EncodeContent Processor with XML

avatar
Expert Contributor

How can I use the output of the Base64EncodeContent (or the Base64Encode function) as input for an XML template, where the flow file content (not an attribute) is the XML with the encoded value included?  There doesn't seem to be a NiFi reader that reads the encoded flow file content, so I'm not sure how best to use this processor aside from using a couple of ReplaceText processors to bracket XML substrings around it.

I appreciate any input.  Thanks for reading.

example:

Initial Flow File: "The quick brown fox jumped over the lazy dog"

Encoded Flow File: VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2c=

 

<?xml version="1.0" encoding="UTF-8" ?>
<a xmlns="some/name/space/goes/here">
   <b>This is a template</b>
   <c>ENCODED DATA GOES HERE</c>
</a>

 

 

1 ACCEPTED SOLUTION

avatar
Expert Contributor

Since ReplaceText can make use of regex groups, I believe you could do something along the lines of

Match text: (.*)
Replace text:<xml> '$1'</xml>

$1 allows you to inject the first regex group you match, which in case of the regex above would match the entire file content. I may be wrong about needing to surround it with single quotes but a quick read of the processor's documentation should clear things up.

This could be a hefty task if you need to load massive files into memory, however I don't believe your encoded strings should pose a problem.

Hope this helps, it's always nice to optimize flows 🙂

View solution in original post

4 REPLIES 4

avatar
Expert Contributor

I believe a ReplaceText where you just match the entire encoded content and then inject it into an xml already written as the replacement value would be the ideal way to do this. 

avatar
Expert Contributor

Are you suggesting it is possible to perform a complete swap of the flow file content using a ReplaceText processor? 

For example: 

If the flow file = "ababababac"

Can I use a single ReplaceText to create <xml>ababababac</xml>?

 

From everything I've read in the docs you can't use the Expression Language to get at the whole of the flow file content, e.g. ${flowFile}

So instead what I do is use 2 ReplaceText processors successively; the first one performs a "Prepend", and the second one performs an "Append".  This technique just bookends (brackets) the flow file content.

 

For example: 

Base64Encode                ReplaceText (prepend)                       ReplaceText(append)

ababababac        ==>      <xml>ababababac            ==>        <xml>ababababac</xml>  

 

But, if you know of a way to do this with a single processor then I'd love to hear your suggestions.

 

Thanks!

avatar
Expert Contributor

Since ReplaceText can make use of regex groups, I believe you could do something along the lines of

Match text: (.*)
Replace text:<xml> '$1'</xml>

$1 allows you to inject the first regex group you match, which in case of the regex above would match the entire file content. I may be wrong about needing to surround it with single quotes but a quick read of the processor's documentation should clear things up.

This could be a hefty task if you need to load massive files into memory, however I don't believe your encoded strings should pose a problem.

Hope this helps, it's always nice to optimize flows 🙂

avatar
Expert Contributor

Brilliant!  That does the trick.  Thanks!