jeudi 23 juin 2016

Extract the element from SOAP xml response using python lxml [on hold]


I am trying to parse the SOAP xml answer from the server using lxml. I am struggling to get the details of one child element, instead of all of them. The response from the server is like this: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <GetAllCompletedScansBasicResponse xmlns="urn:com:spidynamics:webservices:Amp"> <GetAllCompletedScansBasicResult> <ScanBasic> <ID>guid</ID> <Name>string</Name> <ProductType>string</ProductType> <AppType>string</AppType> <AppSubtype>string</AppSubtype> <AppVersion>string</AppVersion> <CreationTime>dateTime</CreationTime> <State>Pending or Running or Suspended or Complete or Failed or Aborted or Offline or Suspended_Manual or Imported or FailedImport or Starting or Suspending or Resuming or Aborting or Importing or Archive_Pending or Archived or Archiving or Deleting or RestorePending or Restoring or FailedRestore or FailedArchive or Not_Started or Blackout_Pending or Blackout_Suspended or Blackout_Suspending or Blackout_Aborted or Suspending_Manual or Unknown</State> <StartUri>string</StartUri> <PolicyID>guid</PolicyID> <PolicyName>string</PolicyName> <CreatorUserName>string</CreatorUserName> <CreatorUserSidString>string</CreatorUserSidString> <ScanStartTime>dateTime</ScanStartTime> <ScanEndTime>dateTime</ScanEndTime> <SensorID>guid</SensorID> <SensorName>string</SensorName> <Priority>short</Priority> <ResultsState>New or Partial or Complete or Imported or FailedImport or Unavailable</ResultsState> <VulnCounts> <CriticalCount>int</CriticalCount> <HighCount>int</HighCount> <MediumCount>int</MediumCount> <LowCount>int</LowCount> <BPCount>int</BPCount> <InfoCount>int</InfoCount> <RiskLevelScore>decimal</RiskLevelScore> </VulnCounts> <SiteID>guid</SiteID> <SiteName>string</SiteName> <ScanType>string</ScanType> <UseAnySensor>boolean</UseAnySensor> <AnalysisType>None or Dynamic or Static or RuntimePTA</AnalysisType> <AnalysisSubType>None or WebSite or WebService</AnalysisSubType> <PublishedState>Unpublished or Published or Publishing or PublishFailed or Processing or ProcessFailed</PublishedState> <PublishedDate>dateTime</PublishedDate> <ParentScanID>guid</ParentScanID> <ChildScanType>None or Retest or BulkRetest</ChildScanType> <AutoArchiveExclude>boolean</AutoArchiveExclude> <DefaultEncoding>string</DefaultEncoding> <EffectiveRights>unsignedInt</EffectiveRights> <Project>guid</Project> <ProjectName>string</ProjectName> <Organization>guid</Organization> <OrganizationName>string</OrganizationName> </ScanBasic> <ScanBasic> <ID>guid</ID> <Name>string</Name> <ProductType>string</ProductType> <AppType>string</AppType> <AppSubtype>string</AppSubtype> <AppVersion>string</AppVersion> <CreationTime>dateTime</CreationTime> <State>Pending or Running or Suspended or Complete or Failed or Aborted or Offline or Suspended_Manual or Imported or FailedImport or Starting or Suspending or Resuming or Aborting or Importing or Archive_Pending or Archived or Archiving or Deleting or RestorePending or Restoring or FailedRestore or FailedArchive or Not_Started or Blackout_Pending or Blackout_Suspended or Blackout_Suspending or Blackout_Aborted or Suspending_Manual or Unknown</State> <StartUri>string</StartUri> <PolicyID>guid</PolicyID> <PolicyName>string</PolicyName> <CreatorUserName>string</CreatorUserName> <CreatorUserSidString>string</CreatorUserSidString> <ScanStartTime>dateTime</ScanStartTime> <ScanEndTime>dateTime</ScanEndTime> <SensorID>guid</SensorID> <SensorName>string</SensorName> <Priority>short</Priority> <ResultsState>New or Partial or Complete or Imported or FailedImport or Unavailable</ResultsState> <VulnCounts> <CriticalCount>int</CriticalCount> <HighCount>int</HighCount> <MediumCount>int</MediumCount> <LowCount>int</LowCount> <BPCount>int</BPCount> <InfoCount>int</InfoCount> <RiskLevelScore>decimal</RiskLevelScore> </VulnCounts> <SiteID>guid</SiteID> <SiteName>string</SiteName> <ScanType>string</ScanType> <UseAnySensor>boolean</UseAnySensor> <AnalysisType>None or Dynamic or Static or RuntimePTA</AnalysisType> <AnalysisSubType>None or WebSite or WebService</AnalysisSubType> <PublishedState>Unpublished or Published or Publishing or PublishFailed or Processing or ProcessFailed</PublishedState> <PublishedDate>dateTime</PublishedDate> <ParentScanID>guid</ParentScanID> <ChildScanType>None or Retest or BulkRetest</ChildScanType> <AutoArchiveExclude>boolean</AutoArchiveExclude> <DefaultEncoding>string</DefaultEncoding> <EffectiveRights>unsignedInt</EffectiveRights> <Project>guid</Project> <ProjectName>string</ProjectName> <Organization>guid</Organization> <OrganizationName>string</OrganizationName> </ScanBasic> </GetAllCompletedScansBasicResult> </GetAllCompletedScansBasicResponse> </soap:Body> </soap:Envelope> I would need to parse and get the information of one <ScanBasic> element, which matches the stated <ID>. How that could be achieved? Thanks a lot! Update: I solved it myself. Turns out you just make a dict with findall. thanks guys for putting this on hold. ScanList = sample_response.findall('.//{urn:com:spidynamics:webservices:Amp}ScanBasic') for element in ScanList: ID = element.find('.//{urn:com:spidynamics:webservices:Amp}ID') if ID.text == "111111": for sub_element in element: print sub_element.text

Aucun commentaire:

Enregistrer un commentaire