Groovy:从xml中删除所有命名空间

时间:2017-11-14 11:31:20

标签: xml groovy apache-nifi

我想从我的xml响应中取代命名空间(现在我在nifi环境中工作,所以我必须使用groovy实用程序) 这是我的代码,但它抛出FlowfFileHandlingException,应该是什么  我改为使这段代码有效吗?:

import org.apache.commons.io.IOUtils
import java.nio.charset.StandardCharsets
import groovy.lang.*
import java.util.concurrent.TimeUnit;
import java.lang.String;
import groovy.xml.XmlUtil



def flowFile=session.get();
 String removeXmlStringNamespaceAndPreamble(String xmlString) {
  return xmlString.replaceAll("(<\\?[^<]*\\?>)?", ""). 
  replaceAll("xmlns.*?(\"|\').*?(\"|\')", "") 
  .replaceAll("(<)(\\w+:)(.*?>)", "${1}${3}") 
  .replaceAll("(</)(\\w+:)(.*?>)", "${1}${3}"); 
  }

 def text = ''
   session.read(flowFile, {inputStream ->
   text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
   } as InputStreamCallback)

//text=removeXmlStringNamespaceAndPreamble(text);
log.error(removeXmlStringNamespaceAndPreamble(text));
  def root = new XmlSlurper( false, false ).parse( text );
  String outxml = groovy.xml.XmlUtil.serialize( root );   
    flowFile = session.write(flowFile, {outputStream ->
   outputStream.write(outxml.getBytes(StandardCharsets.UTF_8))
   }  as OutputStreamCallback)
session.transfer(flowFile,REL_SUCCESS);   

0 个答案:

没有答案