当前位置: 首页 > 知识库问答 >
问题:

使用Python和XML.etree.ElementTree解析XML遇到一些挑战

戚飞虎
2023-03-14
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<feed xml:base="https://share.corp.com/sites/CPIBudget/_vti_bin/ListData.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Tbl_Projects_Tableau</title>
  <id>https://share.corp.com/sites/CPIBudget/_vti_bin/ListData.svc/Tbl_Projects_Tableau/</id>
  <updated>2018-07-25T21:27:59Z</updated>
  <link rel="self" title="Tbl_Projects_Tableau" href="Tbl_Projects_Tableau" />
  <entry m:etag="W/&quot;8&quot;">
    <id>https://share.corp.com/sites/CPIBudget/_vti_bin/ListData.svc/Tbl_Projects_Tableau(1)</id>
    <title type="text"></title>
    <updated>2018-06-14T17:15:27Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="Tbl_Projects_TableauItem" href="Tbl_Projects_Tableau(1)" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/FBN_ID" type="application/atom+xml;type=entry" title="FBN_ID" href="Tbl_Projects_Tableau(1)/FBN_ID" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/CreatedBy" type="application/atom+xml;type=entry" title="CreatedBy" href="Tbl_Projects_Tableau(1)/CreatedBy" />
    <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ModifiedBy" type="application/atom+xml;type=entry" title="ModifiedBy" href="Tbl_Projects_Tableau(1)/ModifiedBy" />
    <category term="Microsoft.SharePoint.DataService.Tbl_Projects_TableauItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:FBN_IDId m:type="Edm.Int32">6</d:FBN_IDId>
        <d:Title m:null="true" />
        <d:PROJECT_NAME>Project Swoop</d:PROJECT_NAME>
        <d:Cluster>ABC</d:Cluster>
        <d:PROJECT_SITE>ABC9</d:PROJECT_SITE>
        <d:PROJECT_ORIGINALAMT m:type="Edm.Double">500000</d:PROJECT_ORIGINALAMT>
        <d:PROJECT_ORG>Nookie</d:PROJECT_ORG>
        <d:PROJECT_GROUP>Smooth</d:PROJECT_GROUP>
        <d:c__OldID m:type="Edm.Double">1</d:c__OldID>
        <d:ContentTypeID>0x0100FD279BEBCF3C4F45BB75D6147D315C09</d:ContentTypeID>
        <d:Id m:type="Edm.Int32">1</d:Id>
        <d:ContentType>Item</d:ContentType>
        <d:Modified m:type="Edm.DateTime">2018-06-14T17:15:27</d:Modified>
        <d:Created m:type="Edm.DateTime">2018-06-14T16:58:50</d:Created>
        <d:CreatedById m:type="Edm.Int32">2</d:CreatedById>
        <d:ModifiedById m:type="Edm.Int32">2</d:ModifiedById>
        <d:Owshiddenversion m:type="Edm.Int32">8</d:Owshiddenversion>
        <d:Version>1.0</d:Version>
        <d:Path>/sites/SmoothBudget/Lists/Projects_Tableau1</d:Path>
      </m:properties>
    </content>
  </entry>
 </feed>

下面是我试图解析为CSV的XML示例。

import config
import csv
import pymysql
import requests
import xml.etree.ElementTree as ET
from requests_ntlm import HttpNtlmAuth

ssoUsername = config.username
ssoPassword = config.password

f = open(path+csvFile,'w',newline='')
csvwriter = csv.writer(f)
column_headers = ['FBN','Project_Name','Cluster','Site','OP2_USD','Type','Group']
csvwriter.writerow(column_headers)

rows = []
r2 = requests.get(project_url, auth=HttpNtlmAuth('ANT\\'+ssoUsername,ssoPassword), verify=False)
projectData = r2.content
etree2 = ET.fromstring(projectData)

#print(etree2.findall('.****'))

for element in etree2.findall(".****") :
    print(element.find('{http://schemas.microsoft.com/ado/2007/08/dataservices}FBN_IDId'))
    fbnKey2 = element.find('{http://schemas.microsoft.com/ado/2007/08/dataservices}FBN_IDId')
    FBN = fbnMap.get(fbnKey2)
[<Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}ContentTypeID' at 0x000001C4B7F31BD8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Id' at 0x000001C4B7F31C28>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}ContentType' at 0x000001C4B7F31C78>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Modified' at 0x000001C4B7F31CC8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Created' at 0x000001C4B7F31D18>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}CreatedById' at 0x000001C4B7F31D68>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}ModifiedById' at 0x000001C4B7F31DB8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Owshiddenversion' at 0x000001C4B7F31E08>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Version' at 0x000001C4B7F31E58>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Path' at 0x000001C4B7F31EA8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}FBN_IDId' at 0x000001C4B7F3ABD8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Title' at 0x000001C4B7F3AB88>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}PROJECT_NAME' at 0x000001C4B7F3AB38>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Cluster' at 0x000001C4B7F3AA98>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}PROJECT_SITE' at 0x000001C4B7F3AA48>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}PROJECT_ORIGINALAMT' at 0x000001C4B7F3A9F8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}PROJECT_ORG' at 0x000001C4B7F3A908>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}PROJECT_GROUP' at 0x000001C4B7F3A868>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}c__OldID' at 0x000001C4B7F3A8B8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}ContentTypeID' at 0x000001C4B7F3A778>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Id' at 0x000001C4B7F3A728>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}ContentType' at 0x000001C4B7F3A6D8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Modified' at 0x000001C4B7F3A138>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Created' at 0x000001C4B7F3A048>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}CreatedById' at 0x000001C4B7F3A638>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}ModifiedById' at 0x000001C4B7F3A5E8>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Owshiddenversion' at 0x000001C4B7F3A548>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Version' at 0x000001C4B7F3A598>, <Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}Path' at 0x000001C4B7F3A4F8>]
None
<Element '{http://schemas.microsoft.com/ado/2007/08/dataservices}FBN_IDId' at 0x000001C4B7F3ABD8>
for element in etree2.findall(".//{http://schemas.microsoft.com/ado/2007/08/dataservices}FBN_IDId") :
        fbnKey2 = element.text
        FBN = fbnMap.get(fbnKey2)

建议?

共有1个答案

莫兴言
2023-03-14

也许您可以使用完整路径:

/feed/entry/content/m:properties/d:FBN_IDId

或:

/feed/entry/content/{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}properties/{http://schemas.microsoft.com/ado/2007/08/dataservices}FBN_IDId
 类似资料:
  • 问题内容: 我为播客提供了一个rss提要,基本上我想做的是在RSS提要中使用URL填充html5音频播放器。 我认为最好的方法是使用ajax解析链接,然后将其附加到音频播放器的src中。我知道相同的域策略会阻止我使用ajax进行此操作,因此我正在使用跨域ajax插件(http://bit.ly/Jbi9iX)来解决此问题。 我竭力要弄清楚究竟为什么下面的代码不工作对我来说,基本上在这个阶段,我只是

  • 本文向大家介绍python xml.etree.ElementTree遍历xml所有节点实例详解,包括了python xml.etree.ElementTree遍历xml所有节点实例详解的使用技巧和注意事项,需要的朋友参考一下 python xml.etree.ElementTree遍历xml所有节点 XML文件内容: 代码: 输出结果: 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

  • 问题内容: 我正在使用Python在Python中生成XML文档,但是在转换为纯文本时,该函数不包含XML声明。 我需要我的字符串包含以下XML声明: 但是,似乎没有任何记录的方式来执行此操作。 有没有合适的方法来呈现XML声明? 问题答案: 我很惊讶地发现似乎没有办法。但是,您可以用来将XML文档写入伪文件: 看到这个问题。即使那样,我认为如果不自己编写,就无法获得“独立”属性。

  • 问题内容: 我编写了一个简单的脚本,用于使用BeautifulSoup模块解析XML聊天日志。标准的soup.prettify()可以正常工作,只是聊天日志中有很多绒毛。您可以在下面看到脚本代码和我正在使用的一些XML输入文件: 码 测试XML输入 我希望能够将其输出为以下格式或至少比纯XML更可读的格式: 乔恩:嘿,怎么了?[10/31/10 @ 3:43p] 乔恩:收到了您的消息[10/31/

  • 本文向大家介绍Python中使用ElementTree解析XML示例,包括了Python中使用ElementTree解析XML示例的使用技巧和注意事项,需要的朋友参考一下 【XML基本概念介绍】 XML 指可扩展标记语言(eXtensible Markup Language)。 XML 被设计用来传输和存储数据。 概念一: 概念二: 概念三: 概念四: 概念五: 概念六: 【XML几种解析方法】

  • 问题内容: 我需要能够使用JavaScript解析XML。XML将在变量中。我宁愿不使用jQuery或其他框架。 我已经看过了,XML> jQuery阅读。 问题答案: 从这个问题的20分钟前的最后一个问题中longitude-to-address),我猜测您正在尝试解析(读取和转换)通过使用GeoNames的FindNearestAddress找到的XML。 如果您的XML在名为的字符串变量中,