Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Archives of Support Questions (Read Only)

This is an archived board for historical reference. Information and links may no longer be available or relevant
Announcements
This board is archived and read-only for historical reference. To ask a new question, please post a new topic on the appropriate active board.

Attributes to XML (not JSON)

avatar
Rising Star

Im looking for a way to get attributes in an XML file instead of json. The current proccesor only does attributes to json.

Any ideas? Maybe using an execute script proccesor? Lua code?

Thank you!

1 ACCEPTED SOLUTION

avatar
Master Guru

Yes it is possible with ExecuteScript if nothing else. Try the following Groovy script in your ExecuteScript processor:

def flowFile = session.get()
if(!flowFile) return
class WriteCallback implements OutputStreamCallback {
  Map attrs
  WriteCallback(attributes) {
    attrs = attributes
  }
  void process(OutputStream outputStream) {
    outputStream.write('<root>\n'.bytes)
    attrs.each {k,v -> 
      outputStream.write("<property>\n\t<name>$k</name>\n\t<value>$v</value>\n".bytes)
    }
    outputStream.write('</root>'.bytes)
  }
}
def wb = new WriteCallback(flowFile.attributes)
flowFile = session.write(flowFile, wb)
flowFile = session.putAttribute(flowFile, org.apache.nifi.flowfile.attributes.CoreAttributes.MIME_TYPE.key(), 'application/xml')
session.transfer(flowFile, REL_SUCCESS)

This should pretty-print your attributes in a "properties-style" XML format. Of course you can edit the script to give you the schema you like.

View solution in original post

2 REPLIES 2

avatar
Master Guru

Yes it is possible with ExecuteScript if nothing else. Try the following Groovy script in your ExecuteScript processor:

def flowFile = session.get()
if(!flowFile) return
class WriteCallback implements OutputStreamCallback {
  Map attrs
  WriteCallback(attributes) {
    attrs = attributes
  }
  void process(OutputStream outputStream) {
    outputStream.write('<root>\n'.bytes)
    attrs.each {k,v -> 
      outputStream.write("<property>\n\t<name>$k</name>\n\t<value>$v</value>\n".bytes)
    }
    outputStream.write('</root>'.bytes)
  }
}
def wb = new WriteCallback(flowFile.attributes)
flowFile = session.write(flowFile, wb)
flowFile = session.putAttribute(flowFile, org.apache.nifi.flowfile.attributes.CoreAttributes.MIME_TYPE.key(), 'application/xml')
session.transfer(flowFile, REL_SUCCESS)

This should pretty-print your attributes in a "properties-style" XML format. Of course you can edit the script to give you the schema you like.

avatar
New Member

I am looking for a way to convert DB Query reusult set to XML and XML to SQL and also JSON to XML. Is this doable?