NIFI:如何在groovy代码中更改xml标记的值?

时间:2017-11-29 12:49:01

标签: xml groovy apache-nifi

我想更改<run>标记值,这是我的xml文件示例:

<?xml version="1.0" encoding="UTF-8"?><service>
  <rs>
    <Cross>
      <details>
        <start>2016-01-05</start>
        <run>true</run>
        <makeVersion>1</makeVersion>
        <patch>this is  patch</patch>
        <parameter>7</parameter>
      </details>
    </Cross>
    <Cost>
      <details>
        <start>2017-09-07</start>
        <run>true</run>
        <makeVersion>1</makeVersion>
        <patch>this is  patch</patch>
        <parameter>1</parameter>
      </details>
    </Cost>
  </rs>

我想用TRUE替换运行值FALSE 这是我的代码,但它不起作用,也不抛出异常(我在nifi环境中运行此代码)

  1. 我应该如何更改代码才能使其正常工作?
  2. 这是我的代码:

    File file = new File("adress  here");
            String content = "";
            BufferedReader s;
            BufferedWriter w;
            RandomAccessFile ini= new RandomAccessFile(file, "rwd");
            FileLock lock= ini.getChannel().lock();
    
            try {
            String sCurrentLine;
                s = new BufferedReader(Channels.newReader(ini.getChannel(), "UTF-8"));
    
    
                while ((sCurrentLine = s.readLine()) != null) {
                    content += sCurrentLine;
                }
                ini.seek(0);
    
    
    
                def xml = new XmlParser().parseText(content)
                 Date  d=new Date();
                GregorianCalendar cal = new GregorianCalendar();
               cal.setTime(d);
                cal.add(Calendar.DATE,-(Integer.valueOf(parameter)));
                 def currentDate=new Date().format( 'yyyy-MM-dd' );
    
                   if(start!=currentDate &&  start!=cal.getTime().format("yyyy-MM-dd") ){
                   def flowFile1=session.create()
                    flowFile1 = session.putAttribute(flowFile1, "filename", "conf.xml");
    
                    session.write(flowFile1, new StreamCallback() {
                        @Override
                        public void process(InputStream inputStream1, OutputStream outputStream) throws IOException {
    
                            outputStream.write(content.getBytes(StandardCharsets.UTF_8))
                        }
    
                    });
    
    
                    session.transfer(flowFile1,REL_SUCCESS);
                    xml.'**'.findAll{it.name() == 'runAs'}.collect{it.replaceBody false}
    
    
                   }
                   /*else{
                   xml.'**'.findAll{it.name() == 'runAs'}.collect{it.replaceBody false}
    
                   }*/
                     def newxml=groovy.xml.XmlUtil.serialize(xml)
    
                String data =newxml;
                if (!data.isEmpty()) {
                    ini.setLength(0);
                    w = new BufferedWriter(Channels.newWriter(ini.getChannel(), "UTF-8"));
                    w.write(data);
                    lock.release();
                    w.close();
                    }
    
    
                   }catch (FileNotFoundException e) {
                //e.printStackTrace();
                TimeUnit.SECONDS.sleep(50000);
            } catch (IOException e) {
                e.printStackTrace();
    
            } catch(OverlappingFileLockException e){ TimeUnit.SECONDS.sleep(50000);
                lock.release();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
    
                //lock.release();
                ini.close();
            }
    

1 个答案:

答案 0 :(得分:1)

如果我理解您正在寻找的内容,请将此行替换为您的代码:

xml.'**'.findAll{it.name() == 'your_node'}.each{ it.replaceBody 'false'}

根据您的问题,将your_node替换为run