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!

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 ...