Sagitarius
Sagitarius

Reputation: 363

NIFI: how to change value of xml tag in groovy code?

I want to change the <run> tag value , here is my xml file example :

<?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>

I WANT TO REPLACE RUN VALUE TRUE WITH FALSE
Here is my code but it does't work and don't throw exception too ( i am running this code inside nifi environment)

  1. How should i change my code to make it work?

here is my code:

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

Upvotes: 0

Views: 617

Answers (1)

Bhanuchander Udhayakumar
Bhanuchander Udhayakumar

Reputation: 1646

If i understand correct what you are looking for, Replace this line with your code:

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

As per your question Here replace your_node as run.

Upvotes: 1

Related Questions