Skip to main content
AndreyDE
Silver ⭐️⭐️⭐️
November 19, 2024
Solved

Nifi: Parse Error for Xml file with 2 doubled tags

Hello!

Xml file has the following structure:

 

 

<root>
<nested1>
<nested2>
<nested1>value</nested1>
</nested2>
</nested1>
</root>

 

 

         EvaluateXPath processor type_rec  property:  name(/*/*[1])

And I got the following error:

      

EvaluateXPath[id=ce4e0c05-169f-387f-520d-77e2e664bacc] Input parsing failed FlowFile[filename=69de5f2c-a887-4042-8734-beca03e3d27b]: org.apache.nifi.xml.processing.ProcessingException: Parsing failed
- Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 5; The element type "nested2" must be terminated by the matching end-tag "</nested2>".

As I understand, parser except, at 4 level closed tag </nested1> , because it'v met open tag with the same name at level 2 (<nested1>).   But at level 4 file has a new tag with the same name. 

I tried to use ReplcaseText processor for replace 

      <nested2>

            <nested1>

tag combination 

with new value - for example:    

<nested2>

            <nested1_new>

So I tried to rename "nested1"  at 4 level with   "nested1_new" value. But without result - nothing is replaced.

It there are other ways to solve this problem?

Thanks.

      

 

 

SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
November 20, 2024

Hi,

Im unable to replicate the error . Can you provide more details about your flow with the processor configurations. here is what I tried and it worked:

 

SAMSAL_0-1732122088279.png

 

GenerateFlowFile:

SAMSAL_1-1732122133158.png

EvaluateXPath:

SAMSAL_2-1732122161817.png

Output flowfile attributes:

SAMSAL_3-1732122206372.png

 

2 replies

AndreyDE
AndreyDEAuthor
Silver ⭐️⭐️⭐️
November 20, 2024

Also I tried to solve this problem, by using ScriptExecutor with following Groovy:

 

 

import org.w3c.dom.Document
import org.w3c.dom.Element
import groovy.xml.XmlSlurper
import groovy.json.JsonBuilder

def xmlContent = '''<root>
<nested1>
<nested2>
<nested2>value</nested2>
</nested2>
</nested1>
</root>'''

def xml = new XmlSlurper().parseText(xmlContent)

def nested2Value = xml.root.nested1.nested2

def json = [
nested2: nested2Value
]

def json2 = [
nested3: nested2
]

def builder = new JsonBuilder(json2)
def xmlOutput = builder.toPrettyString()

I returns:

     {

         "nested3": [

         ]

    }

 

It's not, what I expected, because "value" is missed.  

Translate
SAMSAL
SAMSALAnswer
Platinum ⭐️⭐️⭐️
November 20, 2024

Hi,

Im unable to replicate the error . Can you provide more details about your flow with the processor configurations. here is what I tried and it worked:

 

SAMSAL_0-1732122088279.png

 

GenerateFlowFile:

SAMSAL_1-1732122133158.png

EvaluateXPath:

SAMSAL_2-1732122161817.png

Output flowfile attributes:

SAMSAL_3-1732122206372.png

 

Translate