带有命名空间的Python解析XML文件

时间:2018-11-14 04:11:18

标签: python xml lxml elementtree

我有一个示例xml,在该示例xml下我遇到了解析问题。我发现了一些没有名称空间的示例,但是由于查询空了,因此不确定如何使用名称空间解析某些内容。

下面的代码段中唯一有效的查询是:

print root[1][1].text

有什么想法可以指出正确的方向吗?

XML:

<controls:controls xmlns="http://scap.nist.gov/schema/sp800-53/2.0" xmlns:controls="http://scap.nist.gov/schema/sp800-53/feed/2.0" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" pub_date="2015-02-03T10:11:18.645-05:00" xsi:schemaLocation="http://scap.nist.gov/schema/sp800-53/feed/2.0 http://scap.nist.gov/schema/sp800-53/feed/2.0/sp800-53-feed_2.0.xsd">
<controls:control>
<family>ACCESS CONTROL</family>
<number>AC-1</number>
<title>ACCESS CONTROL POLICY AND PROCEDURES</title>
<priority>P1</priority>
<baseline-impact>LOW</baseline-impact>
<baseline-impact>MODERATE</baseline-impact>
<baseline-impact>HIGH</baseline-impact>
<statement>
<description>The organization:</description>
<statement>
<number>AC-1a.</number>
<description>
Develops, documents, and disseminates to [Assignment: organization-defined personnel or roles]:
</description>
<statement>
<number>AC-1a.1.</number>
<description>
An access control policy that addresses purpose, scope, roles, responsibilities, management commitment, coordination among organizational entities, and compliance; and
</description>
</statement>
<statement>
<number>AC-1a.2.</number>
<description>

Python:

import xml.etree.ElementTree as ET
tree = ET.parse('controls.xml')
root = tree.getroot()

#gives output
print root[1][1].text


ns = {'d': 'http://scap.nist.gov/schema/sp800-53/2.0'}
for family in root.findall('d:family', ns):
    family1 = country.find('d:family', ns).text
    family2 = country.find('d:priority', ns).text
    print(family2, family1)

0 个答案:

没有答案