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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...