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
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...