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!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer Certification at ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

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