代码之家  ›  专栏  ›  技术社区  ›  Rahul

解析从internet派生的XML文件

  •  0
  • Rahul  · 技术社区  · 6 年前

    <feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xml:base="https://my325415.crm.ondemand.com/sap/byd/odata/v1/c4c.svc/">
    <id>
    https://xxxxx.xxxx.com/sap/byd/odata/v1/c4c.svc/https://xxxxxx.xxxx.com/sap/byd/odata/v1/c4c.svc/OpportunityCollection/?$format=xml
    </id>
    <title type="text">OpportunityCollection</title>
    <updated>2018-09-19T19:58:11Z</updated>
    <author>
    <name/>
    </author>
    <link href="OpportunityCollection" rel="self" title="OpportunityCollection"/>
    <entry>
    <id>
    https://myxxxx.xxxx.ondemand.com/sap/byd/odata/v1/c4c.svc/OpportunityCollection('xxxxxxxx')
    </id>
    <title type="text">Test Syracuse Zoo - Opp</title>
    <updated>2018-09-19T19:58:11Z</updated>
    <category term="xxxxxx" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
    <link href="OpportunityCollection('xxxxxxxxx')" rel="edit" title="Opportunity"/>
    <link href="OpportunityCollection('xxxxxxxx')/OpportunityProduct" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/OpportunityProduct" type="application/atom+xml;type=feed" title="OpportunityProduct"/>
    <link href="OpportunityCollection('xxxxxxxxx')/OpportunitySalesTeam" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/OpportunitySalesTeam" type="application/atom+xml;type=feed" title="OpportunitySalesTeam"/>
    <link href="OpportunityCollection('xxxxxxxx')/OpportunityInvolvedParty" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/OpportunityInvolvedParty" type="application/atom+xml;type=feed" title="OpportunityInvolvedParty"/>
    <link href="OpportunityCollection('xxxxxxxxxxx')/Task" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Task" type="application/atom+xml;type=feed" title="Task"/>
    <content type="application/xml">
    <m:properties>
    <d:ObjectID>xxxxxxxxxxxxx</d:ObjectID>
    <d:ExpectedRevenueAmount m:type="c4c.Amount">
    <d:currencyCode>USD</d:currencyCode>
    <d:content>0.00</d:content>
    </d:ExpectedRevenueAmount>
    <d:ProbabilityPercent>25</d:ProbabilityPercent>
    <d:Name>Test Syracuse Zoo - Opp</d:Name>
    <d:AccountName>DO NOT USE 3 - DOMESTIC</d:AccountName>
    <d:StartDate>2016-11-21T00:00:00</d:StartDate>
    <d:CloseDate>2017-02-27T00:00:00</d:CloseDate>
    <d:SalesCycle>Z1</d:SalesCycle>
    <d:SalesCycleText>JMA - General Opportunity</d:SalesCycleText>
    <d:SalesPhase>Z2</d:SalesPhase>
    <d:SalesPhaseText>Budgetary Quote - 25%</d:SalesPhaseText>
    <d:ReasonForStatus>Z24</d:ReasonForStatus>
    <d:ReasonForStatusText>Active Opportunity</d:ReasonForStatusText>
    <d:Source>Z9</d:Source>
    <d:SourceText>Sales Referral - from Anixter</d:SourceText>
    <d:PrimaryContact>Test First Name Test Last Name</d:PrimaryContact>
    <d:ID>21</d:ID>
    <d:ChangedOn>2017-03-31T02:10:29.3910740Z</d:ChangedOn>
    <d:Status>2</d:Status>
    <d:StatusText>In Process</d:StatusText>
    <d:AccountID>100002150</d:AccountID>
    <d:OwnerID>8000000011</d:OwnerID>
    <d:CRMUILink>
    https://xxxxx.xxxx.xxxxx.com/sap/public/byd/runtime?bo=COD_OPPORTUNITY_THINGTYPE&nav_mode=TI&param.Key=00163E0813301ED6AC8355A5C1DA8831
    </d:CRMUILink>
    <d:ExternalID>00000000000000000000000000000000001</d:ExternalID>
    <d:Owner>Sandy Blau</d:Owner>
    <d:DaysUntilClosing>-569</d:DaysUntilClosing>
    </m:properties>
    </content>
    </entry>
    <link rel="next" href="https://my325415.crm.ondemand.com/sap/byd/odata/v1/c4c.svc/OpportunityCollection/?$format=xml&$skiptoken=101%20"/>
    </feed>
    

    下面是我用来解析它的代码:

    import xml.etree.ElementTree as ET
    
    xmlp = ET.XMLParser(encoding = "utf-8")
    
    
    xmldoc=ET.parse('foot.xml',parser=xmlp)
    root = xmldoc.getroot()
    print(root)
    

    此代码的问题是,它只读取提要标记并提供以下输出:

    <Element '{http://www.w3.org/2005/Atom}feed' at 0x0000000002D7B0E8>
    

    我想得到所有子节点之间的数据,而不必硬编码元素名称。我只关心 .

    由于这个文件是从互联网上提取的,我没有办法改变输入数据的流动方式。

    1 回复  |  直到 6 年前
        1
  •  0
  •   it's-yer-boy-chet    6 年前

    您看到的是由内置函数返回的Element对象的字符串表示 报告 方法,而不是根树的内容。

    要查看对象中的标记,请尝试:

    for child in root:
        print child.tag, child.attrib
    

    docs 有关访问特定标记的详细信息。