Python:读取特定标记下的子节点的所有值并输出它们

时间:2016-10-21 20:24:10

标签: xml xml-parsing python-3.5

您好我想在标签'c:Dto'下阅读子元素的所有值? 我可以将字符串'c:Dto'作为常量或硬编码传递给它。没有使用xml.etree.ElementTree或xml.dom.minidom模块的约束。下面是我正在研究的xml 你能帮我解决这个问题吗?

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
      <ReadResponse xmlns="http://schemas.planview.com/PlanviewEnterprise/Services/OutcomeService/2016/10">
         <ReadResult xmlns:a="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteResult/2010/01/01" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:Failures xmlns:b="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteStatus/2010/01/01"/>
            <a:GeneralErrorMessage i:nil="true"/>
            <a:Successes xmlns:b="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteStatus/2010/01/01">
               <b:OpenSuiteStatus i:type="c:OutcomeStatus" xmlns:c="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/AuthorizationStatus/2013/08">
                  <b:Code i:nil="true"/>
                  <b:ErrorMessage i:nil="true"/>
                  <b:SourceIndex>0</b:SourceIndex>
                  <c:Dto xmlns:d="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/Dtos/OutcomeDto/2016/10">
                     <d:CreatedByUserKey>key://3/mdavis</d:CreatedByUserKey>
                     <d:CreatedOn>2016-10-19T10:42:19.163</d:CreatedOn>
                     <d:Description>Outcome thru SOAP 14768903041476890304147689030414</d:Description>
                     <d:FatherKey>key://2/$Prod/26850</d:FatherKey>
                     <d:InvestmentApprovalKey>key://2/Wbs27/WBS27$ANLZ</d:InvestmentApprovalKey>
                     <d:Key>key://2/$Prod/26896</d:Key>
                     <d:LifecycleAdminUserKey>key://3/snadmin</d:LifecycleAdminUserKey>
                     <d:OutcomeId>abcdefghijkl</d:OutcomeId>
                     <d:OutcomeStatusKey>key://2/PBS2/PBS2$CNCL</d:OutcomeStatusKey>
                     <d:OutcomeTypeKey>key://2/PBS8/26856</d:OutcomeTypeKey>
                     <d:TargetFinishDate i:nil="true"/>
                     <d:TargetStartDate i:nil="true"/>
                  </c:Dto>
               </b:OpenSuiteStatus>
            </a:Successes>
            <a:Warnings xmlns:b="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteStatus/2010/01/01"/>
         </ReadResult>
      </ReadResponse>
    </s:Body>
    </s:Envelope>

1 个答案:

答案 0 :(得分:0)

您可以使用minidom,这是一个示例(如果您使用的是xml文件,则需要按parseString更改parse):

from xml.dom import minidom

xmlString = """<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
      <ReadResponse xmlns="http://schemas.planview.com/PlanviewEnterprise/Services/OutcomeService/2016/10">
         <ReadResult xmlns:a="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteResult/2010/01/01" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:Failures xmlns:b="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteStatus/2010/01/01"/>
            <a:GeneralErrorMessage i:nil="true"/>
            <a:Successes xmlns:b="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteStatus/2010/01/01">
               <b:OpenSuiteStatus i:type="c:OutcomeStatus" xmlns:c="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/AuthorizationStatus/2013/08">
                  <b:Code i:nil="true"/>
                  <b:ErrorMessage i:nil="true"/>
                  <b:SourceIndex>0</b:SourceIndex>
                  <c:Dto xmlns:d="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/Dtos/OutcomeDto/2016/10">
                     <d:CreatedByUserKey>key://3/mdavis</d:CreatedByUserKey>
                     <d:CreatedOn>2016-10-19T10:42:19.163</d:CreatedOn>
                     <d:Description>Outcome thru SOAP 14768903041476890304147689030414</d:Description>
                     <d:FatherKey>key://2/$Prod/26850</d:FatherKey>
                     <d:InvestmentApprovalKey>key://2/Wbs27/WBS27$ANLZ</d:InvestmentApprovalKey>
                     <d:Key>key://2/$Prod/26896</d:Key>
                     <d:LifecycleAdminUserKey>key://3/snadmin</d:LifecycleAdminUserKey>
                     <d:OutcomeId>abcdefghijkl</d:OutcomeId>
                     <d:OutcomeStatusKey>key://2/PBS2/PBS2$CNCL</d:OutcomeStatusKey>
                     <d:OutcomeTypeKey>key://2/PBS8/26856</d:OutcomeTypeKey>
                     <d:TargetFinishDate i:nil="true"/>
                     <d:TargetStartDate i:nil="true"/>
                  </c:Dto>
               </b:OpenSuiteStatus>
            </a:Successes>
            <a:Warnings xmlns:b="http://schemas.planview.com/PlanviewEnterprise/OpenSuite/OpenSuiteStatus/2010/01/01"/>
         </ReadResult>
      </ReadResponse>
    </s:Body>
    </s:Envelope>"""

xmldoc = minidom.parseString(xmlString)

xmlDto = xmldoc.getElementsByTagName('c:Dto')

dtoKey = xmlDto[0].getElementsByTagName('d:FatherKey')
print (dtoKey[0].childNodes[0].nodeValue)
  

输出:key://2/$Prod/26850