Hi.
I am using telegraf to parse my xml data. I am having a requirement to get the node name and the value. However i am not able to get the node name and i am able to get the child name. Below is Xml data and telegraf configuration:
Xml-data:
<?xml version="1.0" encoding="UTF-8"?>
<report>
<sampleduration>900</sampleduration>
<network>
<gateway>
<red>103</red>
<blue>105</blue>
<black>120</black>
</gateway>
</network>
</report>
Following is the telegraf-configuration:
[[inputs.file.xpath]]
field_selection = "//gateway"
field_name = "name(//gateway)"
field_value = "string(//gateway)"
Actual output:
gateway = "103
105
120"
Expected output:
red = 103,blue = 105,black = 120
Also i tried below but its not working.
field_name = “//gateway/*/name()”
Hi @Pratik_Das_Baghel,
So I spoke with one of our Telegraf maintainers who managed to solve your issue. Here is the breakdown:
- This is correct as you are selecting the base level which does not include the inner text of the gateway.
- To grab all leaf nodes then you need to perform a
descendant::*
. This selects all child nodes irrespective of their level. The config would look like this:
[[inputs.file.xpath]]
metric_selection = "//report"
metric_name = "string('report')"
field_selection = "descendant::*[not(*)]"
Note: (
[not(*)] )
filters those nodes for the ones that do not have children themselves.
Hi @Jay_Clifford. I was having another query, can you help me here.
<?xml version="1.0" encoding="UTF-8"?>
<report>
<sampleduration>900</sampleduration>
<network>
<gateway>
<red><element>103</element></red>
<blue><element>105</element></blue>
<black><element>120</element></black>
</gateway>
</network>
</report>
This time i am getting a common node say element, which i have to ignore. I am sure that the inside nodes will be having same name (here its element). So the expected output is:
red = 103,blue = 105,black = 120
How can i achieve this?
Thanks