简体   繁体   English

使用 Python 打印解析的 XML 时遇到问题

[英]Trouble printing out parsed XML using Python

Im having trouble figuring out why my second print statement doesn't print anything out, please HELP!我无法弄清楚为什么我的第二个打印语句没有打印出任何内容,请帮助!

My first print statement print (root[0][1].text) is able to parse the xml file and print out AC-1 while the other print statement below the for loop for varible in root.iter('number'):print(varible.text) doesn't seem to find the data at all.我的第一个打印语句print (root[0][1].text)能够解析 xml 文件并打印出AC-1而 for 循环下面的另一个打印语句for varible in root.iter('number'):print(varible.text)似乎根本找不到数据。

ParseXML.py解析XML文件

filePath='/Users/userName/Desktop/xmlFile.xml'
tree = ET.parse(filePath)
root = tree.getroot()
#This print stmt prints 'AC-1'
print (root[0][1].text)
for varible in root.iter('number'): 
        #This print stmt doesn't print anything
        print(varible.text)

xmlFile.xml xml文件.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>Procedures to facilitate the implementation of the access control policy and associated access controls; and
                    </description>
                </statement>
            </statement>
            <statement>
                <number>AC-1b.</number>
                <description>Reviews and updates the current:</description>
                <statement>
                    <number>AC-1b.1.</number>
                    <description>
                    Access control policy [Assignment: organization-defined frequency]; and
                    </description>
                </statement>
                <statement>
                    <number>AC-1b.2.</number>
                    <description>
                    Access control procedures [Assignment: organization-defined frequency].
                    </description>
                </statement>
            </statement>
        </statement>
        <supplemental-guidance>
            <description>
            This control addresses the establishment of policy and procedures for the effective implementation of selected security controls and control enhancements in the AC family. Policy and procedures reflect applicable federal laws, Executive Orders, directives, regulations, policies, standards, and guidance. Security program policies and procedures at the organization level may make the need for system-specific policies and procedures unnecessary. The policy can be included as part of the general information security policy for organizations or conversely, can be represented by multiple policies reflecting the complex nature of certain organizations. The procedures can be established for the security program in general and for particular information systems, if needed. The organizational risk management strategy is a key factor in establishing policy and procedures.
            </description>
            <related>PM-9</related>
        </supplemental-guidance>
        <references>
            <reference>
                <item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-12">NIST Special Publication 800-12</item>
            </reference>
            <reference>
                <item xml:lang="en-US" href="https://csrc.nist.gov/publications/search?keywords-lg=800-100">NIST Special Publication 800-100</item>
            </reference>
        </references>
    </controls:control>
</controls:controls>

Your XML file is such as all tags are preceded by " {http://scap.nist.gov/schema/sp800-53/2.0} ".您的 XML 文件例如所有标签都以“ {http://scap.nist.gov/schema/sp800-53/2.0} ”开头。

Try out this piece of code to test it:试试这段代码来测试它:

    root = tree.getroot()

    for item in root.iter('{http://scap.nist.gov/schema/sp800-53/2.0}number'):
       print(item.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM