Splunk Search

Loop through XML Child Elements and Concatenate

bansi
Path Finder

We use Log4J log file which is fed as input to Splunk. Each entry in the XML file is XML object with timestamp.

Our requirement is to write a search the Log file and extract key fields and then use Splunk Build Report feature to Export CSV file

The challenge is to get the Child Elements of a XML Node and then concatenate them with comma delimiter.

For example here is the entry from log file

2010-11-17 12:59:24,617 [main] DEBUG splunk - marshallObjectToXml; <?xml version="1.0" encoding="UTF-8" standalone="yes"?><EventLogData xmlns="http:/xyz/EventLogData"><Data screen-name="ScottTiger"><DataNode node-type="Contract">
<Elements>
                    <Name name-type="Name">true</Name>
    <DateOfBirth>true</DateOfBirth>         <PhoneNo>true</PhoneNo  
</Elements>


</DataNode></Data></EventLogData>

Wondering if Splunk provides a way how to write a search query which will result in following output

Name, DateOfBirth,PhoneNo
Tags (1)
0 Karma

ziegfried
Influencer

First you have to extract those values you're interested in. This can be done either through xpath or the rex command. Using a search like this, would bring the results in tabular form that can be exported:

sourcetype=<your sourcetype> 
| rex "(?m)\<Name[^\>]*>(?<Name>[^\<]+)\</Name\>.*\<DateOfBirth[^\>]*>(?<DateOfBirth>[^\<]+)\</DateOfBirth\>.*\<PhoneNo[^\>]*>(?<PhoneNo>[^\<]+)\</PhoneNo\>"
| table Name DateOfBirth PhoneNo
0 Karma

bansi
Path Finder

Hi Ziegfried,
Thanks for your response. I tried your rex command and the search result displays the values under those tag elements whereas my requirement is to extract tag names and not the values under those tag elements.
Please note tag names are dynamic in nature i.e. one xml might have tag names : "Name DateOfBirth PhoneNo" whereas other xml might have tag names :"Gender Name PhoneNo"
Once i retrieve the list of tag names under tag , i have to concatenate theses tag names with comma separated.
For example my search query should return ouput as
Name, DateOfBirth, PhoneNo

0 Karma
Get Updates on the Splunk Community!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...