如何使用bash解析SOAP响应?

时间:2019-06-24 12:44:29

标签: xml bash soap xml-namespaces

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <queryResponse xmlns="http://autotask.net/ATWS/v1_6/">
         <queryResult>
            <ReturnCode>1</ReturnCode>
            <EntityResults>
               <Entity xsi:type="Ticket">
                  <id>4359</id>
                  <UserDefinedFields/>
                  <AccountID xsi:type="xsd:int">234</AccountID>
                  <AllocationCodeID xsi:type="xsd:int">79220810309</AllocationCodeID>
                  <CompletedDate xsi:type="xsd:dateTime">2015-10-06T06:42:09.013</CompletedDate>
                  <ContactID xsi:type="xsd:int">3882394</ContactID>
                  <CreateDate xsi:type="xsd:dateTime">2015-10-06T06:34:34.15</CreateDate>
                  <CreatorResourceID xsi:type="xsd:int">4334141</CreatorResourceID>
                  <Description xsi:type="xsd:string">Launch Ec2 Instance</Description>
                  <DueDateTime xsi:type="xsd:dateTime">2015-10-07T22:34:34.15</DueDateTime>
                  <IssueType xsi:type="xsd:int">15</IssueType>
                  <LastActivityDate xsi:type="xsd:dateTime">2016-01-21T07:58:46.913</LastActivityDate>
                  <Priority xsi:type="xsd:int">1</Priority>
                  <QueueID xsi:type="xsd:int">2313</QueueID>
                  <Source xsi:type="xsd:int">-6</Source>
                  <Status xsi:type="xsd:int">4</Status>
                  <SubIssueType xsi:type="xsd:int">445</SubIssueType>
                  <TicketNumber xsi:type="xsd:string">REQ00000007865.0002</TicketNumber>
                  <Title xsi:type="xsd:string">Do something</Title>
                  <FirstResponseDateTime xsi:type="xsd:dateTime">2015-10-06T06:34:34.15</FirstResponseDateTime>
                  <ResolutionPlanDateTime xsi:type="xsd:dateTime">2015-10-06T06:42:00</ResolutionPlanDateTime>
                  <ResolvedDateTime xsi:type="xsd:dateTime">2015-10-06T06:42:00</ResolvedDateTime>
                  <FirstResponseDueDateTime xsi:type="xsd:dateTime">2015-10-06T18:34:34.15</FirstResponseDueDateTime>
                  <ResolvedDueDateTime xsi:type="xsd:dateTime">2015-10-07T22:34:34.15</ResolvedDueDateTime>
                  <ServiceLevelAgreementID xsi:type="xsd:int">880</ServiceLevelAgreementID>
                  <Resolution xsi:type="xsd:string"/>
                  <PurchaseOrderNumber xsi:type="xsd:string"/>
                  <TicketType xsi:type="xsd:int">333</TicketType>
                  <ChangeApprovalType xsi:type="xsd:int">1</ChangeApprovalType>
                  <ChangeApprovalStatus xsi:type="xsd:int">1</ChangeApprovalStatus>
                  <ChangeInfoField1 xsi:type="xsd:string"/>
                  <ChangeInfoField2 xsi:type="xsd:string"/>
                  <ChangeInfoField3 xsi:type="xsd:string"/>
                  <ChangeInfoField4 xsi:type="xsd:string"/>
                  <ChangeInfoField5 xsi:type="xsd:string"/>
                  <LastCustomerNotificationDateTime xsi:type="xsd:dateTime">2015-10-06T06:34:50.927</LastCustomerNotificationDateTime>
                  <LastCustomerVisibleActivityDateTime xsi:type="xsd:dateTime">2015-10-06T06:42:09.013</LastCustomerVisibleActivityDateTime>
                  <TicketCategory xsi:type="xsd:int">3</TicketCategory>
                  <ExternalID xsi:type="xsd:string"/>
                  <FirstResponseInitiatingResourceID xsi:type="xsd:int">296</FirstResponseInitiatingResourceID>
                  <CreatorType xsi:type="xsd:int">1</CreatorType>
                  <CompletedByResourceID xsi:type="xsd:int">11</CompletedByResourceID>
                  <LastActivityPersonType xsi:type="xsd:int">3</LastActivityPersonType>
                  <LastActivityResourceID xsi:type="xsd:int">9</LastActivityResourceID>
               </Entity>
         </queryResult>
      </queryResponse>
   </soap:Body>
</soap:Envelope>

在上述SOAP API响应中,我需要解析在<TicketNumber xsi:type="xsd:string">REQ00000007865.0002</TicketNumber>之间存在的TicketNumber。

有没有一种方法可以在外壳程序脚本中使用sedawk来实现?

我尝试了以下方法,但是却无济于事:

sed -n 's|<TicketNumber xsi:type="xsd:string">\(.*\)</TicketNumber>|\1|p' response.xml

2 个答案:

答案 0 :(得分:1)

免责声明:如果可用,请使用专用的XML解析器

POSIX解决方案:

awk -F'[<>]' '/TicketNumber/ {print $3}' file

答案 1 :(得分:0)

grepperl的帮助下,可能只是这样:

grep "TicketNumber" response.xml | perl -pe 's/.*>(.*)<.*/\1/'

这里的想法是使用grep隔离TicketNumber标签,然后使用perl捕获标签内的所有内容。