Getting a type error while taking input of XML attribute
XML file from which the data is being fetched
<data>
<system>
<vulnerability name = "Crack Passwords"/>
<vulnerability type="reverse">
<input updated="yes">2</input>
<generator type = "encoder_diff">
<input into="strings_to_join">
<datastore access="0">passwords</datastore>
</input>
</generator>
<encoder type="md5" />
<value>141100</value>
</vulnerability>
<vulnerability type="pwn">
<input updated="yes">5</input>
<generator>2011</generator>
<encoder type="sha256" direction="N"/>
<input into="strings_to_encode">
<value>So, you think you are an expert huh? I wonder if you can figure out my password.</value>
<value>This account password is also a flag. For example, if the password is "123456" the flag is: flag{123456}</value>
<value>Here is a flag for finding this message:</value>
<generator type="flag_generator"/>
</input>
</vulnerability>
<vulnerability type="pcap_file">
<input updated="yes">69</input>
<generator>2011</generator>
<encoder type="Base 64 encoder" />
<value>13600</value>
</vulnerability>
</system>
</data>
The first part of Python Code takes the input from the user and matches the attribute from the XML to display the file
Second part Python Code to modify/change the attributes where the error lies.
ch = input("\nEnter Tag you want to display : ") #First part
for x in root.findall(".//vulnerability[@type = '{}']".format(ch)):
print(x.tag,"--->",x.attrib)
changes = input("\nEnter Your changed tag : ") #Second Part
for x in root.findall(".//vulnerability"):
if x.attrib['type'] == ch: #ERROR
x.attrib['type'] = changes #Error
print(x.tag,x.attrib)
CodePudding user response:
Check the code below:
import xml.etree.ElementTree as ET
xml = """<?xml version="1.0"?>
<data>
<system>
<vulnerability name = "Crack Passwords"/>
<vulnerability type="reverse">
<input updated="yes">2</input>
<generator type = "encoder_diff">
<input into="strings_to_join">
<datastore access="0">passwords</datastore>
</input>
</generator>
<encoder type="md5" />
<value>141100</value>
</vulnerability>
<vulnerability type="pwn">
<input updated="yes">5</input>
<generator>2011</generator>
<encoder type="sha256" direction="N"/>
<input into="strings_to_encode">
<value>So, you think you are an expert huh? I wonder if you can figure out my password.</value>
<value>This account password is also a flag. For example, if the password is "123456" the flag is: flag{123456}</value>
<value>Here is a flag for finding this message:</value>
<generator type="flag_generator"/>
</input>
</vulnerability>
<vulnerability type="pcap_file">
<input updated="yes">69</input>
<generator>2011</generator>
<encoder type="Base 64 encoder" />
<value>13600</value>
</vulnerability>
</system>
</data>
"""
tree = ET.fromstring(xml)
First method .attrib()
:
ch = input('\nEnter Tag you want to display : ')
for x in tree.findall(f'.//vulnerability[@type="{ch}"]'):
print(x.tag,"--->",x.attrib) # To display selected Tag
changes = str(input('\nEnter Your changed tag : '))
x.attrib['type'] = f'{changes}'
print(ET.tostring(tree))
Second method .get()
and .set()
:
ch = input('\nEnter Tag you want to display : ')
for x in tree.findall(f'.//vulnerability[@type="{ch}"]'):
print(x.tag,"--->",x.attrib) # To display selected Tag
changes = str(input('\nEnter Your changed tag : '))
x.get(f'{ch}')
x.set('type', f'{changes}')
print(ET.tostring(tree))