Sagitarius
Sagitarius

Reputation: 363

get XML tag names?

I want to get tag names from xml response and put this data in flowfile1, but for getting all child node name I will have to convert my response data into xml Document, but I get errors on getChildren().

Here is my code:

import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets


def  flowFile=session.get();
def flowFile1=session.create();
def tagList="";
session.read(flowFile, {inputStream ->
  text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
} as InputStreamCallback)
def  xml=new XmlParser().parseText(text) 
xml=xml as  Document;
for tag  in xml.findChildren(){
   tagList+=tag+ "\n";
}
flowFile1=session.putAttribute(flowFile1,"filename","tagList");
flowFile1 = session.write(flowFile1, {outputStream ->
   outputStream.write(tagList.getBytes(StandardCharsets.UTF_8))
   }  as OutputStreamCallback)


session.transfer(flowFile1,REL_SUCCESS);
session.remove(flowFile);

Here is an example of the response XML:

<responseDate>
    <person>
        <name>
        </name>
        <id>
        </id>
    </person>
</responseDate>

And in flowfile1 I want to write data like this:

 responseData
 person
 name  
 id

Upvotes: 0

Views: 534

Answers (1)

Bhanuchander Udhayakumar
Bhanuchander Udhayakumar

Reputation: 1621

I hope this will help you .

def xml = new XmlParser().parseText(text)
xml.'**'.each { 
println it.name()
}

Upvotes: 1

Related Questions