获取 XML 个标签名称?

get XML tag names?

我想从 xml 响应中获取标签名称并将此数据放入 flowfile1,但是为了获取所有子节点名称,我必须将我的响应数据转换为 xml 文档,但是我在 getChildren().

上出现错误

这是我的代码:

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);

这是响应的示例 XML:

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

而在 flowfile1 中,我想这样写数据:

 responseData
 person
 name  
 id

希望对您有所帮助。

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