# Parsing an xml file

**URL:** <https://discuss.elastic.co/t/parsing-an-xml-file/76971>\
**Category:** Logstash\
**Created:** [March 1, 2017, 11:35am UTC](https://discuss.elastic.co/t/parsing-an-xml-file/76971 "2017-03-01T11:35:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mohamed](https://discuss.elastic.co/u/Mohamed)\
**Post date:** [March 1, 2017, 11:35am UTC](https://discuss.elastic.co/t/parsing-an-xml-file/76971/1 "2017-03-01T11:35:40Z")

</div>

Hello,  
I am having a sample xml log file like below:

`<note date="2017-03-01"> <to>XX</to> <from>YY</from> </note>`

I want to parse the above code and want to display the data in an output file like this:

`<note> <date> <year>2017</year> <month>03</month> <day>01</day> <date> <to fullName="XX">Destination</to> <from fullName="YY">Source</from> </note>`

I am new to ELK framework . I have tried to create logstash config using xpath filters but no luck if anyone help me with sample code means it would be a great help.

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:** [March 1, 2017, 12:55pm UTC](https://discuss.elastic.co/t/parsing-an-xml-file/76971/2 "2017-03-01T12:55:42Z")

</div>

If you show what you have come up with so far (even if it doesn't work) it'll be easier to help out.

---

<div class="post-metadata">

**Author:** ![Mohamed](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mohamed](https://discuss.elastic.co/u/Mohamed)\
**Post date:** [March 1, 2017, 1:41pm UTC](https://discuss.elastic.co/t/parsing-an-xml-file/76971/3 "2017-03-01T13:41:09Z")

</div>

input {  
file {  
path =\> "/tmp/test/test.xml"  
start\_position =\> "beginning"  
sincedb\_path =\> "NUL"  
codec =\> multiline {  
pattern =\> " `<note>|</note>`"  
negate =\> true  
what =\> "previous"  
auto\_flush\_interval =\> 1  
}  
}  
}

filter {  
if [message] == "`<note>`" or [message] == "`</note>`" {

#here I want to copy at their places  
target =\> "message.parsed"

}

xml {  
source =\> "message"  
target =\> "message.parsed"  
xpath =\> ["/event/message/text()", eventmessage]  
force\_array =\> false  
}  
}

output {  
file {  
path =\> "/tmp/test/result.xml" }  
}

it's all that I could do.. I can't find a way to do that transformation.

---

<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:** [March 1, 2017, 2:04pm UTC](https://discuss.elastic.co/t/parsing-an-xml-file/76971/4 "2017-03-01T14:04:09Z")

</div>

> ```
> if [message] == "<note>" or [message] == "</note>" {
> 
> ```

This condition will never be true because `message` will never contain only an opening or a closing tag.

> ```
> xpath => ["/event/message/text()", eventmessage]
> 
> ```

You're mixing up the Logstash event with the XML snippet. You're probably looking for something like this:

```plaintext
xpath => [
  "/note/from/text()", "from,
  "/note/to/text()", "to,
  "/note/@date", "date"
]

```

Note that Logstash doesn't have an XML output plugin, so while it's possible to produce the XML snippet you want I don't recommend it. Why not just use an XSLT template for this XML transform?

---

<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:** [March 29, 2017, 2:04pm UTC](https://discuss.elastic.co/t/parsing-an-xml-file/76971/5 "2017-03-29T14:04:17Z")

</div>

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