Support Questions

Find answers, ask questions, and share your expertise

Preserving the parent element in XML document with XMLReader

avatar
Expert Contributor

When using the XMLReader service, how do you retain the outermost parent element of an XML document so when I convert it to JSON I have the outermost element in the JSON version too?

 

For example, given the following XML document how do I capture the "<a>" element rather than just the "<b>" element?  

 

<a>
<b>45</b>
</a>

 

What I want:

{"a": {"b":45}}

 

What I get:

{"b":45}

 

2 ACCEPTED SOLUTIONS

avatar
Super Guru

@ChuckE ,

 

You can use a JoltTransformRecord processor to perform the conversion to JSON and, at the same time, add the root node back to it.

 

araujo_0-1659503928628.png

The JOLT specification I used is this:

{
  "*": "a.&"
}

 

Cheers,

André

 

 

--
Was your question answered? Please take some time to click on "Accept as Solution" below this post.
If you find a reply useful, say thanks by clicking on the thumbs up button.

View solution in original post

avatar
Expert Contributor

I've since discovered a super easy way to resolve this.  Simply using the XMLRecordSetWriter does EXACTLY what I was looking for.  

View solution in original post

11 REPLIES 11

avatar
Expert Contributor

I create an XMLRecordSetWriter in the Controller Services, then using a ConvertRecord processor I'm able to read the xml record and then immediately write it out with a new root tag, which I can then pass to my next processor.  I discovered this when I was reading the documentation for the XMLRecordSetWriter.  Very first line in the documentation. 😃

https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-record-serialization-services...

 

avatar
Super Guru

Ahh! Good catch, @ChuckE !! So simple!

araujo_0-1659580409302.png

 

--
Was your question answered? Please take some time to click on "Accept as Solution" below this post.
If you find a reply useful, say thanks by clicking on the thumbs up button.