# Update xml file content to prevent xml parse error

**URL:** <https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050>\
**Category:** Logstash\
**Created:** [December 11, 2017, 9:36am UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050 "2017-12-11T09:36:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![iam\_thadiyan](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@iam\_thadiyan](https://discuss.elastic.co/u/iam_thadiyan)\
**Post date:** [December 11, 2017, 9:36am UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/1 "2017-12-11T09:36:37Z")

</div>

I have a number of (~10k) XML files that have a missing closing root tag. When I load this file to logstash with xml filter I get parse error.

Is there a way to make logstash add a closing tag in filter before parsing the content?

Given below is my XML:

```
 <root>
   <elm>....</elm>
   <elm>....</elm>
   <elm>....</elm>
   <elm>....</elm>

```

I looked at filters and couldn't figure out how to add closing root tag to the read content. Please help.

---

<div class="post-metadata">

**Author:** ![iam\_thadiyan](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@iam\_thadiyan](https://discuss.elastic.co/u/iam_thadiyan)\
**Post date:** [December 11, 2017, 12:05pm UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/2 "2017-12-11T12:05:26Z")

</div>

Is there a way where in I can access the file contents from input { } in filter { } section? If I can write some ruby code to modify the read file contents?

Thanks.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [December 11, 2017, 1:23pm UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/3 "2017-12-11T13:23:03Z")

</div>

You can modify the XML payload with e.g. a mutate filter (the gsub option might be useful) before you feed it to the xml filter.

---

<div class="post-metadata">

**Author:** ![iam\_thadiyan](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@iam\_thadiyan](https://discuss.elastic.co/u/iam_thadiyan)\
**Post date:** [December 11, 2017, 2:43pm UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/4 "2017-12-11T14:43:10Z")

</div>

Magnus, thank s a lot for replying. gsub does a substitution - right? How can I make it to append closing tag to the source? I am very sorry, I fail to see what you are saying.

---

<div class="post-metadata">

**Author:** ![iam\_thadiyan](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@iam\_thadiyan](https://discuss.elastic.co/u/iam_thadiyan)\
**Post date:** [December 11, 2017, 2:47pm UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/5 "2017-12-11T14:47:24Z")

</div>

I tried to use merge to merge two fields. But I result in an array containing both values.

```
input {
     file {
     .....
     add_field => { 'closing_tag' => '</root>' }
     .....
}

filter {
     mutate {
            merge => { 'message' => 'closing_tag' }
     } 
     .......

```

The above create an array with two values - one with the message content and the second with closing\_tag.

What am I missing here?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [December 11, 2017, 2:57pm UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/6 "2017-12-11T14:57:36Z")

</div>

> How can I make it to append closing tag to the source?

Replace `$` (the end of the string) with `</root>` (using gsub) or do this:

```plaintext
mutate {
  replace => {
    "message" => "%{message}</root>"
  }
}

```

---

<div class="post-metadata">

**Author:** ![iam\_thadiyan](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@iam\_thadiyan](https://discuss.elastic.co/u/iam_thadiyan)\
**Post date:** [December 11, 2017, 3:12pm UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/7 "2017-12-11T15:12:10Z")

</div>

Yes!! Thanks a lot for your help. It worked. I was stuck at this for days and you solved it in a minute. You are awesome. 👍

---

<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:** [January 8, 2018, 3:12pm UTC](https://discuss.elastic.co/t/update-xml-file-content-to-prevent-xml-parse-error/111050/8 "2018-01-08T15:12:23Z")

</div>

Closed on January 8, 2018, 3:12pm UTC

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