in Education by
In my first class i parse my xml file like this : class ListActeurs(): fonction_key = "DiagramElements" def __init__(self): self.list_acteurs = [] root = ElementTree.parse("D:\\Users\T0211254\MyApp\Bundle CUD-CAPELLA 431\melody\eclipse\workspace\XML2\XML2.aird").getroot() for diagram in self.root: if ListActeurs.diagramme_key in diagram.tag: self.diagram_name = diagram.attrib.get('name') self.diagram_id = diagram.get('{http://www.omg.org/XMI}id') self.dict_diagrams[self.diagram_name] = self.diagram_id for owned in diagram: if ListActeurs.acteur_key in owned.tag: self.acteur_name = owned.attrib.get('name') # Récupération des noms des acteurs self.acteur_id = owned.get('{http://www.omg.org/XMI}id') # Récupération des id's des acteurs print(self.acteur_name, '==>', self.acteur_id) for elements in owned: if ListActeurs.fonction_key in elements.tag: self.fonctions_name = elements.attrib.get('name') # Récupération des noms des fonctions self.fonctions_id = elements.get('{http://www.omg.org/XMI}id') # Récupération des ID's des fonctions self.data_fonctions = ElementTree.tostring(elements) self.list_acteurs.append(Acteur(self.fonctions_name, self.fonctions_id, self.data_fonctions)) At the end i create an Object Acteur() with differents attributes including "self.data_fonction" which contains a part of the xml file in a String variable. I would like to continue to parse this part of xml in another class. I tried this : class Acteur() : def __init__(self, fonctions_name, fonctions_id, data_fonctions): self.fonctions_name = str(fonctions_name) self.fonctions_id = str(fonctions_id) self.data_fonctions = str(data_fonctions) elements = ET.ElementTree(ET.fromstring(data_fonctions)) for ports in elements : ports_name = ports.attrib.get('name') print(ports_name) But there is an error : for ports in elements : TypeError: 'ElementTree' object is not iterable However I can't use the ElementTree fonction parse() because it's not anymore a file. How can i do that ? I have thinking of creating an xml file and to put "self.data_fonction" in it et to parse this xml file but is there any better method to do this ? Thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
According to documentation: https://docs.python.org/2/library/xml.etree.elementtree.html While parsing xml file you have created ElementTree object. You have to use instance of ElementTree object ('root' in your code example) in order to iterate. So basically just use: for child in root: print child.tag, child.attrib instead of: elements = ET.ElementTree(ET.fromstring(data_fonctions)) for ports in elements : ports_name = ports.attrib.get('name') print(ports_name)

Related questions

0 votes
    What is break, continue and pass in Python?...
asked Dec 6, 2020 in Technology by JackTerrance
0 votes
    What is the purpose continue statement in python?...
asked Nov 25, 2020 in Technology by JackTerrance
0 votes
    I have no clue why is this getting failed datetime.datetime.strptime( date_string, ' %A %d %B %Y : %I:%M %p ... help me solve this Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have the following XML file that I'm trying to iterate through using xml.etree: SUCCESS 1234 12 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Which class allows parsing of command line arguments? (a) Args (b) JCommander (c) Command Line (d) ... programming questions and answers pdf, java interview questions for beginners...
asked Oct 26, 2021 in Education by JackTerrance
0 votes
    I got this question regarding web server (such as nginx, Cherokee or Oracle iPlanet) and Java containers ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I am trying to POST a HTML (contained in a file) to a URL using Wget like this: wget -O- ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I'm using the progress bar of WPF and set the value until the max value. But, when reached, the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    What is the difference between break statement and continue statement ? Select the correct answer from above options...
asked Dec 28, 2021 in Education by JackTerrance
0 votes
    Analyse the given program segment and answer the following questions i) write the output of the program segment ii)how many times ... body of the loop gets execute for(int m=5 ; m...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    Read the paragraph and answer the questions given below: Reproduction is the process by which the living species ... higher organisms? Select the correct answer from above options...
asked Nov 9, 2021 in Education by JackTerrance
0 votes
    If an index is _________________ the metadata and statistics continue to exists (a) Disabling (b) Dropping (c) ... in SQL in portion Indexing and Hashing of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    The transaction can no longer continue with its normal execution because of some internal condition, such ... topic in chapter Recovery System of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    The system has entered an undesirable state (for example, deadlock), as a result of which a ... Failure Classification in division Recovery System of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
...