# How to use ruby code to index some variable element from XML node

**URL:** <https://discuss.elastic.co/t/how-to-use-ruby-code-to-index-some-variable-element-from-xml-node/131429>\
**Category:** Logstash\
**Created:** [May 11, 2018, 8:25am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-index-some-variable-element-from-xml-node/131429 "2018-05-11T08:25:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrymu](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@terrymu](https://discuss.elastic.co/u/terrymu)\
**Post date:** [May 11, 2018, 8:25am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-index-some-variable-element-from-xml-node/131429/1 "2018-05-11T08:25:32Z")

</div>

Hi expert,

I have used the logstash to parse some information with XML format, such like this:

```
<?xml version="1.0" encoding="UTF-8"?>
<OMeS version="2.3">
  <PMSetup startTime="2018-03-09T07:50:00.000+01:00" interval="5">
    <PMMOResult>
      <MO dimension="network_element">
        <DN>NTAS-tas01/HOSTNAME-cbam-4c63de7aeae2460289cd4197dc7-admintd-node-0/DBTYPE-cmdb</DN>
      </MO>
      <PMTarget measurementType="DBMEAS">
        <M704B1C1>32</M704B1C1>
        <M704B1C2>216</M704B1C2>
        <M704B1C3>0</M704B1C3>
        <M704B1C4>0</M704B1C4>
      </PMTarget>
    </PMMOResult>
 <PMMOResult>
      ... ...
 </PMMOResult>
     ... ... 

```

I have used xml plugin to parsed them.

```
xml {
                        source => "message"
                        target => "parsed"
}

```

Then the parsed result such like this:

```
parsed.PMSetup   
 {
      "startTime": "2018-03-09T07:50:00.000+01:00",
      "interval": "5",
      "PMMOResult": [
        {
          "MO": [
            {
              "DN": [
                "NTAS-tas01/HOSTNAME-cbam-4c63de7aeae2460289cd4197dc7-admintd-node-0/DBTYPE-cmdb"
              ],
              "dimension": "network_element"
            }
          ],
          "PMTarget": [
            {
              "measurementType": "DBMEAS",
              "M704B1C3": [
                "0"
              ],
              "M704B1C2": [
                "216"
              ],
              "M704B1C4": [
                "0"
              ],
              "M704B1C1": [
                "32"
              ]
            }
          ]
        },

```

Now my major work is want to add some indexes to use retrieve the xml data. such like this:

 ![Capture](https://us1.discourse-cdn.com/elastic/original/3X/e/2/e25b8a2ae2b632c0661150709670d8447f77e6a7.png)

How can I use ruby to add these indexes to these points(which includes node name, node attribute and node text)?

I have tried to write some ruby codes such like this: but it doesn't work.

```
ruby { code => "
		rec = []

		pmMeasInfo = event['parsed']['PMSetup'][0]['PMMOResult']
		node = pmMeasInfo['MO']['DN'][0]
		pmType = pmMeasInfo['PMTarget'].attributes["measurementType"]

		pmMeasInfo.each do |m1|
			ValueType = m1.name
			pmValue = m1['PMtarget']

```

Would you please help us check it? thank you.

---

<div class="post-metadata">

**Author:** ![terrymu](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@terrymu](https://discuss.elastic.co/u/terrymu)\
**Post date:** [May 11, 2018, 8:26am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-index-some-variable-element-from-xml-node/131429/2 "2018-05-11T08:26:56Z")

</div>

@magnusbaeck  
expert, would you please help me check this question? thank you.

---

<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:** [June 8, 2018, 8:27am UTC](https://discuss.elastic.co/t/how-to-use-ruby-code-to-index-some-variable-element-from-xml-node/131429/3 "2018-06-08T08:27:03Z")

</div>

Closed on June 8, 2018, 8:27am UTC

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