Important Update: Archer Community Scheduled Maintenance on November 23–24 - New Community Launching Soon! Learn More..
2024-09-12 03:19 PM
Hi - I am trying to use the SearchRecordsByReport call for getting the report data from archer. Did anyone use python to parse the XML response? If so, can you pls suggest ?
I tried below and I am getting Bad Request
from xml.etree import ElementTree as ET
response = self._session.post(url, headers=headers, data=data, timeout=self._service_timeout)
response.raise_for_status()
root = ET.fromstring(response.content)
return root
2024-09-17 02:59 AM
Hi, Try this code which helps you to get the Records from the report and the filed details of the fields in the report.
import requests
from xml.etree import ElementTree as ET
from lxml import etree
import pandas as pd
#SearchRecordsByReportId
def search_records_by_report(SessionToken,choice):
try:
url ="URL here"
report_id="Enter the Report id here"
pgno="Enter the Page number" #1
soap_envelope ="""
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<SearchRecordsByReport xmlns="http://archer-tech.com/webservices/">
<sessionToken>{}</sessionToken>
<reportIdOrGuid>{}</reportIdOrGuid>
<pageNumber>{}</pageNumber>
</SearchRecordsByReport>
</soap12:Body>
</soap12:Envelope>""".format(SessionToken,report_id,pgno)
headers ={"Content-Type": "application/soap+xml; charset=utf-8"}
response = requests.post(url, data=soap_envelope, headers=headers)
print("Search Records By Report Post Response status code",response.status_code)
if response.status_code == 200:
Rs_content=response.content
root = etree.fromstring(Rs_content)
ns = {'ns': 'http://archer-tech.com/webservices/'}
result = root.xpath("//ns:SearchRecordsByReportResult/text()", namespaces=ns)
search_records_by_report_result=result[0]
field_root = ET.fromstring(search_records_by_report_result)
Record_definition = field_root.find("Record")
Metadata_definition = field_root.find("Metadata")
AllRecords = field_root.findall("Record")
fields_details = []
for Metadata in Metadata_definition:
for fieldDefinition in Metadata:
field_id = fieldDefinition.attrib['id']
field_name = fieldDefinition.attrib['name']
field_Guid = fieldDefinition.attrib['guid']
fields_details.append({'Field_id': field_id, 'Field_name': field_name,'Field_Giud':field_Guid})
for record in Record_definition:
for field in fields_details:
if record.attrib['id'] == field['Field_id']:
field['Field_type'] = record.attrib['type']
AllRecords_list=[]
for e_record in AllRecords:
E_Record={}
for data in e_record:
for field in fields_details:
if data.attrib['id']==field['Field_id']:
E_Record[field['Field_name']]=data.text
AllRecords_list.append(E_Record)
AllRecords_dataframe=pd.DataFrame(AllRecords_list)
if choice==2: #use this choice to get the field details like type and Guid etc.
result_to_send =fields_details
elif choice==3: # use this choice to get the All Records in the report
result_to_send=AllRecords_dataframe
else:
print("Error in Terminating the session. Status code:", response.status_code)
except Exception as e:
print(f'An error occurred: {e}')
return result_to_send
SearchRecordsByReportresult=search_records_by_report(Ss_token,choice)
print("Search Records By Report Result is:\n",SearchRecordsByReportresult)