Dashboards & Visualizations

How to parse XML Output with the Splunk Python SDK into a structure that I can use to insert into another system?

ygkr
New Member
import splunklib.client as client

HOST = "XXXXS"
PORT = XXXX
USERNAME = "admin"
PASSWORD = "XXXXXX"


service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)


for app in service.apps:
    print (app.name)
kwargs_export = {"search_mode": "normal"}
searchquery_export = "search sourcetype=db| stats count by 

Priority"
exportsearch_results = service.jobs.export(searchquery_export, 

**kwargs_export)
 # Get the results and display them using the ResultsReader
print (exportsearch_results.read())
#yield {'Priority': exportsearch_results.read()}

Output:

alt text

I have attempted to parse the XML output into a structure so that I can use to insert into another system.
I need a structured output with columns of Priority and its corresponding count value.
Is there any way to access values with python?

0 Karma

Treep
Explorer

Have a look at the search/jobs/export endpoint documentation:
http://docs.splunk.com/Documentation/Splunk/6.5.0/RESTREF/RESTsearch#search.2Fjobs.2Fexport

You could use the GET request parameter "output_mode" and set the output mode to json or csv, which both are - in my opinion - way easier to parse into Python-useable structures than XML.

Other than that, you can use one of the many available XML libraries for Python e.g. those mentioned in this Stackoverflow question/answers:
http://stackoverflow.com/questions/1912434/how-do-i-parse-xml-in-python

Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...