这是我第一次尝试用 python 解析 XML,所以答案可能很简单,但我想不通。
我正在使用 ElementTree 来解析一些 XML 文件。问题是当我拥有这个属性时,我无法在树内得到任何结果:
<package xmlns="http://apple.com/itunes/importer" version="software5.1">
删除此属性时,一切正常。需要明确的是,我的意思是将 XML 文件的第一行更改为:
<package>
一切都很好。
我究竟做错了什么?
这是我的代码:
import xml.etree.ElementTree as ET
tree = ET.parse('metadataCopy.xml')
root = tree.getroot()
p = root.find(".//intervals/interval")
print p
for interval in root.iterfind(".//intervals/interval"):
start_date = interval.find('start_date').text
end_date = interval.find('end_date').text
print start_date, end_date
请帮忙。谢谢!
更新:XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://apple.com/itunes/importer" version="software5.1">
<metadata_token>TOKEN</metadata_token>
<provider>Provider Name</provider>
<team_id>Team_ID_Here</team_id>
<software>
<!--Apple ID: 01234567-->
<vendor_id>vendorSKU</vendor_id>
<read_only_info>
<read_only_value key="apple-id">01234567</read_only_value>
</read_only_info>
<software_metadata>
<versions>
<version string="1.0">
<locales>
<locale name="en-US">
<title>title text</title>
<description>Description text</description>
<keywords>
<keyword>key1</keyword>
<keyword>key2</keyword>
</keywords>
<version_whats_new>New things here</version_whats_new>
<support_url>http://someurl.com</support_url>
<software_screenshots>
<software_screenshot display_target="iOS-3.5-in" position="1">
</software_screenshot>
<software_screenshot display_target="iOS-4-in" position="1">
</software_screenshot>
</software_screenshots>
</locale>
</locales>
</version>
</versions>
<products>
<product>
<territory>WW</territory>
<cleared_for_sale>true</cleared_for_sale>
<sales_start_date>2013-01-05</sales_start_date>
<intervals>
<interval>
<start_date>2013-08-25</start_date>
<end_date>2014-09-01</end_date>
<wholesale_price_tier>5</wholesale_price_tier>
</interval>
<interval>
<start_date>2014-09-01</start_date>
<wholesale_price_tier>6</wholesale_price_tier>
</interval>
</intervals>
<allow_volume_discount>true</allow_volume_discount>
</product>
</products>
</software_metadata>
</software>