【问题标题】:get XML tag names?获取 XML 标记名称?
【发布时间】:2017-11-28 04:48:01
【问题描述】:

我想从 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

【问题讨论】:

    标签: xml groovy apache-nifi


    【解决方案1】:

    希望对你有帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多