如何使用 python 从 xml 检索 src 属性?
How to retrieve the src attribute from an xml using python?
您好,我正在使用 python 小程序搜索 xml 中的所有标签。访问标签后,我想访问所有 src 属性。我如何在 python 中执行此操作?以下是我的 xml 定义。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<smil systemRequired="pss6"
xmlns="http://www.w3.org/2001/SMIL20/Language"
xmlns:pss6="http://www.3gpp.org/SMIL20/PSS6/">
<head>
<meta id="meta-smil1.0-a" name="Publisher" content="OMA"/>
<layout>
<root-layout width="100%" height="100%"/>
<region id="UP" top="0%" left="0%" height="50%" width="100%" fit="meet" backgroundColor="white"/>
<region id="DOWN" top="50%" left="0%" height="50%" width="100%" fit="meet" backgroundColor="white"/>
<region id="FULL" top="0%" left="0%" height="100%" width="100%" fit="meet" backgroundColor="white"/>
</layout>
</head>
<body>
<par index="0" dur="10" size="3326">
<img src="f5d226a7-487f-4038-8051-d9382acde16f" region="DOWN" fill="freeze" size="3310"/>
<text src="tess" region="UP" size="16"/>
</par>
<par index="1" dur="10" size="19534">
<img src="7556e55d-52c7-4807-9034-d6abee06ce67" region="DOWN" fill="freeze" size="2796"/>
<text src="bigno" region="UP" size="20"/>
<audio src="84620751-25db-4db9-b361-43a3dfd70f21" size="16718"/>
</par>
</body>
</smil>
以下是我的python程序。
import xml.etree.ElementTree as ET
def parse():
tree = ET.parse('smil.xml')
root = tree.getroot()
for img in root.findall('./body/par/img'):
print(img)
if __name__ == '__main__':
parse()
我想要的是检索所有 img 标签,然后检索 src 属性并将属性值存储在列表中。
所以我期望的输出是一个列表 = [f5d226a7-487f-4038-8051-d9382acde16f,7556e55d-52c7-4807-9034-d6abee06ce67 ] 因为在 xml 中我们有两个标签 .
我怎样才能实现它?
谢谢
您必须在路径中指定元素的 namespace。
def parse():
tree = ET.parse('smil.xml')
root = tree.getroot()
ns = {"x": "http://www.w3.org/2001/SMIL20/Language"}
for img in root.findall('.//x:img', namespaces=ns):
print(img.attrib['src'])
但是,如果您熟悉 XPath,我建议您查看 lxml 以解析 XML 和 HTML。
您好,我正在使用 python 小程序搜索 xml 中的所有标签。访问标签后,我想访问所有 src 属性。我如何在 python 中执行此操作?以下是我的 xml 定义。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<smil systemRequired="pss6"
xmlns="http://www.w3.org/2001/SMIL20/Language"
xmlns:pss6="http://www.3gpp.org/SMIL20/PSS6/">
<head>
<meta id="meta-smil1.0-a" name="Publisher" content="OMA"/>
<layout>
<root-layout width="100%" height="100%"/>
<region id="UP" top="0%" left="0%" height="50%" width="100%" fit="meet" backgroundColor="white"/>
<region id="DOWN" top="50%" left="0%" height="50%" width="100%" fit="meet" backgroundColor="white"/>
<region id="FULL" top="0%" left="0%" height="100%" width="100%" fit="meet" backgroundColor="white"/>
</layout>
</head>
<body>
<par index="0" dur="10" size="3326">
<img src="f5d226a7-487f-4038-8051-d9382acde16f" region="DOWN" fill="freeze" size="3310"/>
<text src="tess" region="UP" size="16"/>
</par>
<par index="1" dur="10" size="19534">
<img src="7556e55d-52c7-4807-9034-d6abee06ce67" region="DOWN" fill="freeze" size="2796"/>
<text src="bigno" region="UP" size="20"/>
<audio src="84620751-25db-4db9-b361-43a3dfd70f21" size="16718"/>
</par>
</body>
</smil>
以下是我的python程序。
import xml.etree.ElementTree as ET
def parse():
tree = ET.parse('smil.xml')
root = tree.getroot()
for img in root.findall('./body/par/img'):
print(img)
if __name__ == '__main__':
parse()
我想要的是检索所有 img 标签,然后检索 src 属性并将属性值存储在列表中。
所以我期望的输出是一个列表 = [f5d226a7-487f-4038-8051-d9382acde16f,7556e55d-52c7-4807-9034-d6abee06ce67 ] 因为在 xml 中我们有两个标签 .
我怎样才能实现它? 谢谢
您必须在路径中指定元素的 namespace。
def parse():
tree = ET.parse('smil.xml')
root = tree.getroot()
ns = {"x": "http://www.w3.org/2001/SMIL20/Language"}
for img in root.findall('.//x:img', namespaces=ns):
print(img.attrib['src'])
但是,如果您熟悉 XPath,我建议您查看 lxml 以解析 XML 和 HTML。