Splunk Search

Regex for extracting a xml node from a xml feild

ssekar
Engager

Hello Expert,

Please help me arrive on a regex to extract a xml node in a xml field.

I have a field value like below

<Reponse status="failure">
 <messages>
        <message id="Payload">
            <UpdateAccountRq>
                <AccountId>123465</AccountId>
                <NewStatus>Active</NewStatus>
            </UpdateAccountRq>
        </message>
    </messages>
</Reponse>
 
And I want to extract the below xml node and display it in a separate field.
 
 <UpdateAccountRq>
        <AccountId>123465</AccountId>
       <NewStatus>Active</NewStatus>
</UpdateAccountRq>
 
I tried many ways, but nothing works.
 
Attempt 1:  rex field=Action "messages>(?<Payload>.+)<\/messages" | table Action, Payload
Attempt 2:  rex field=Action "\<message id=\"Payload\">(?<Payload>[^<\/message]+)" | table Action, Payload
 
Please help. Thanks
Labels (2)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

@yuanliu is correct, but for a pure rex solution, try this regex

\<message id=\"Payload\">(?<Payload>[\s\S]+?)\<\/message

 

---
If this reply helps you, Karma would be appreciated.

View solution in original post

ssekar
Engager

Thanks for the help. All three are good answers. 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| rex field=Action "(?ms)messages>(?<Payload>.+)<\/messages"

richgalloway
SplunkTrust
SplunkTrust

@yuanliu is correct, but for a pure rex solution, try this regex

\<message id=\"Payload\">(?<Payload>[\s\S]+?)\<\/message

 

---
If this reply helps you, Karma would be appreciated.

yuanliu
SplunkTrust
SplunkTrust

If your document is conformant XML, you should use builtin commands such as spath; regex will be difficult to maintain if the format, or syntax, or schema change.  In your case, assuming you have a field named "xml", you can do

 

| spath input=xml path=Reponse.messages.message.UpdateAccountRq

 

Your sample data gives the following

Reponse.messages.message.UpdateAccountRqxml
<AccountId>123465</AccountId> <NewStatus>Active</NewStatus><Reponse status="failure"> <messages> <message id="Payload"> <UpdateAccountRq> <AccountId>123465</AccountId> <NewStatus>Active</NewStatus> </UpdateAccountRq> </message> </messages> </Reponse>

 

Tags (1)
Get Updates on the Splunk Community!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...