0

I put together a simple Python script to print out XML data for all package names that are associated with parent element attribute: Security Advisory.

import xml.etree.ElementTree as ET
tree = ET.parse('errata.xml')
root = tree.getroot()
for security in root.findall("*[@type='Security Advisory']"):
    packages = security.find('packages')
    print(packages.text)

The XML data is located here

However, the script only prints out the first package name but there are multiple package names. How would I go about getting all the package names that fall under parent attribute: Security Advisory?

3
  • Maybe you find findall instead of find? Commented Sep 14, 2020 at 12:29
  • @larsks Changed this: packages = security.findall('packages') Received this error: print(packages.text) AttributeError: 'list' object has no attribute 'text' Commented Sep 15, 2020 at 19:00
  • That's correct. Because now you're getting a list instead of a single item, of course. Commented Sep 15, 2020 at 19:18

1 Answer 1

1

below (it seems to work)

import xml.etree.ElementTree as ET

import requests

r = requests.get('https://cefs.b-cdn.net/6010e333a44911e24b5112e23acbb346ae15f7b7/errata.latest.xml')
if r.status_code == 200:
    root = ET.fromstring(r.content)
    sec_elements = [e for e in root.findall("*[@type='Security Advisory']") if
                    e.find('os_release') is not None and int(e.find('os_release').text) > 6]
    for ele in sec_elements:
        packages = ele.findall('./packages')
        for p in packages:
            print(p.text)
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for this! This is much better since it pulls the XML directly. Why did you use: ('./packages') instead of: ('packages')?
@alteredstate I think both should work the same so I dont have a good explanation for that...
Would it be possible to take this one step further and print out the package list based on the 'Security Advisory' as well as the: os_release element text? Here is a small sample of the XML: <CEBA-2020--0749> <os_arch>i686</os_arch> <os_arch>x86_64</os_arch> <os_release>6</os_release> <packages>nfs-utils-1.2.3-78.el6_10.2.i686.rpm</packages> <packages>nfs-utils-1.2.3-78.el6_10.2.src.rpm</packages> <packages>nfs-utils-1.2.3-78.el6_10.2.x86_64.rpm</packages> </CEBA-2020--0749>
What do you want to have? Do you want to find the packages if CEBA elements that their type is @type='Security Advisory' AND the os_release element has a specific value? Try to clarify.
Yes, exactly! Print out package list based on @type='Security Advisory' (as we have done) and os_release > 6.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.