亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python:從 xml 文件構建不同的路徑/樹

Python:從 xml 文件構建不同的路徑/樹

PHP
慕哥9229398 2023-11-09 21:17:23
以下是 xml 文件的示例:<?xml version="1.0" encoding="utf-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">  <SOAP-ENV:Header />  <SOAP-ENV:Body>    <ADD_LandIndex_001>      <CNTROLAREA>        <BSR>          <status>ADD</status>          <NOUN>LandIndex</NOUN>          <REVISION>001</REVISION>        </BSR>      </CNTROLAREA>      <DATAAREA>        <LandIndex>          <reportId>AMI100031</reportId>          <requestKey>R3278458</requestKey>          <SubmittedBy>EN4871</SubmittedBy>          <submittedOn>2015/01/06 4:20:11 PM</submittedOn>          <LandIndex>            <agreementdetail>              <agreementid>001       4860</agreementid>              <agreementtype>NATURAL GAS</agreementtype>              <currentstatus>                <status>ACTIVE</status>                <statuseffectivedate>1965/02/18</statuseffectivedate>                <termdate>1965/02/18</termdate>              </currentstatus>              <designatedrepresentative>              </designatedrepresentative>            </agreementdetail>          </LandIndex>        </LandIndex>      </DATAAREA>    </ADD_LandIndex_001>  </SOAP-ENV:Body></SOAP-ENV:Envelope>我想將 xml 文件中包含文本的所有不同路徑存儲在列表中。所以我想要這樣的東西:['Envelope/Body/ADD_LandIndex_01/CNTROLAREA/BSR/status', 'Envelope/Body/ADD_LandIndex_01/CNTROLAREA/BSR/LandIndex', ...]我嘗試了一些不起作用的代碼。我不知道如何單獨獲取一個分支的最后一個元素,以及當我在中間切換節點時如何從頭開始所有路徑(即Envelope/Body/ADD_LandIndex_01/DATAAREA...import xml.etree.ElementTree as etimport osimport pandas as pdfrom re import searchfilename = 'file_try.xml'element_tree = et.parse(filename)root = element_tree.getroot()namespace = "{http://schemas.xmlsoap.org/soap/envelope/}"def remove_namespace(string,namespace) :        if search(namespace, string) :        new_string = string.replace(namespace,'')    else :         new_string= string    return new_string誰能幫我 ?
查看完整描述

1 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

您可以根據實際代碼修改它,但基本上 - 它應該如下所示:


from lxml import etree

soap = """[your xml above]"""

root = etree.XML(soap.encode())    

tree = etree.ElementTree(root)

for target in root.xpath('//text()'):

    if len(target.strip())>0:       

        print(tree.getpath(target.getparent()).replace('SOAP-ENV:',''))

輸出:


/Envelope/Body/ADD_LandIndex_001/CNTROLAREA/BSR/status

/Envelope/Body/ADD_LandIndex_001/CNTROLAREA/BSR/NOUN

/Envelope/Body/ADD_LandIndex_001/CNTROLAREA/BSR/REVISION

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/reportId

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/requestKey

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/SubmittedBy

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/submittedOn

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/LandIndex/agreementdetail/agreementid

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/LandIndex/agreementdetail/agreementtype

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/LandIndex/agreementdetail/currentstatus/status

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/LandIndex/agreementdetail/currentstatus/statuseffectivedate

/Envelope/Body/ADD_LandIndex_001/DATAAREA/LandIndex/LandIndex/agreementdetail/currentstatus/termdate



查看完整回答
反對 回復 2023-11-09
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號