简体   繁体   中英

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.

ParseXML.py

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

<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} ".

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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