# Best way to remove CDATA Infos in xml

**URL:** <https://discuss.elastic.co/t/best-way-to-remove-cdata-infos-in-xml/164470>\
**Category:** Logstash\
**Created:** [January 16, 2019, 1:04pm UTC](https://discuss.elastic.co/t/best-way-to-remove-cdata-infos-in-xml/164470 "2019-01-16T13:04:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![humalog](https://avatars.discourse-cdn.com/v4/letter/h/cc9497/32.png) [@humalog](https://discuss.elastic.co/u/humalog)\
**Post date:** [January 16, 2019, 1:04pm UTC](https://discuss.elastic.co/t/best-way-to-remove-cdata-infos-in-xml/164470/1 "2019-01-16T13:04:05Z")

</div>

Hello,

I have an XML with several CDATA Info in my values. I don't want them there. What is the best way to delete this infos? I tried with using filter { mutate { gsub }} but is there an easier solution?

Here is an example of my fields:

```
CVE_ID: <![CDATA[CVE-2018-13217]]>, <![CDATA[CVE-2018-12498]]>, <![CDATA[CVE-2018-184362]]>
Threat: <![CDATA[Firefox is a free open-scource web browser.]]>

```

What i want:

```
CVE_ID: CVE-2018-13217, CVE-2018-12498, CVE-2018-184362
Threat: Firefox is a free open-source web-browser.

```

My filter for this:

```
filter{
  ....
    mutate { 
        gsub => [
            "CVE_ID", "<!\[CDATA", "",
            "Threat", "<!\[CDATA", ""
        ]
    }
}

```

I am pretty sure there is a better solution for my problem, can somone help me?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [January 16, 2019, 3:55pm UTC](https://discuss.elastic.co/t/best-way-to-remove-cdata-infos-in-xml/164470/2 "2019-01-16T15:55:20Z")

</div>

Well, you could do it using gsub, which would be like

```
mutate {
    gsub => [
        "CVE_ID", "<!\[CDATA\[", "",
        "Threat", "<!\[CDATA\[", "",
        "CVE_ID", "]]>", "",
        "Threat", "]]>", ""
    ]
}

```

but you would have to add entries for every field you want to modify. What we need is a way to specify that these gsubs be done on a list of fields. You can do that using a ruby script. Error handling left as an exercise for the reader. In your logstash configuration, add

```
ruby {
    path => "/home/user/stripCDATA.rb"
    script_params => { "fields" => ["CVE_ID", "Threat"] }
}

```

And in the rb file save this:

```auto
def register(params)
  @fields = params['fields']
end

def filter(event)
  @fields.each { |x|
    a = event.get(x)
    if a then
        a = a.gsub("<!\[CDATA\[", "")
        a = a.gsub("]]>", "")
        event.set(x, a)
    end
  }
  [event]
end

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 13, 2019, 3:55pm UTC](https://discuss.elastic.co/t/best-way-to-remove-cdata-infos-in-xml/164470/3 "2019-02-13T15:55:21Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
