python - XML file parsing - Get data from each parent and their own children -


i data each parent , own children fro xml file.

i'm trying parse xml file

<db>     <entry>         <name>assembly.iam</name>         <displayname>assembly.iam</displayname>         <scalar>             <name>d0</name>             <displayname>d0 (value = 0 mm)</displayname>             <value>0</value>         </scalar>         <scalar>              <name>d1</name>              <displayname>d1 (value = 0 mm)</displayname>         <value>0</value>         </scalar>     </entry>     <entry>         <name>ground.ipt</name>         <displayname>ground.ipt</displayname>         <scalar>             <name>ground_length</name>             <displayname>ground_length (value = 160 mm)</displayname>             <value>160</value>         </scalar>         <scalar>             <name>d2</name>             <displayname>d2 (value = 80 mm)</displayname>             <value>80</value>         </scalar>     </entry> </db> 

in fact, data <displayname></displayname>. then, put data array of tuples this

[(assembly.iam,[d0 (value = 0 mm), d1 (value = 0 mm)]), (ground.ipt,[ground_length (value = 160 mm), d2 (value = 80 mm)]) 

i have tried use xml.etree.celementtree library code

from xml.etree import celementtree import numpy np  workingdir = "c:/users/vince/test" newstrworkingdir = str.replace(workingdir, '/', '\\') tree = celementtree.parse(newstrworkingdir + "\\test.xml") root = tree.getroot() tab = np.empty(shape=(0, 0)) tabentry = np.empty(shape=(0, 0)) tabscalar = np.empty(shape=(0, 0))  entry in root.findall('entry'):     entrynames = entry.findall("./displayname")     entrynamestext = entry.find("./displayname").text     tabentry = np.append(tabentry,entrynamestext)     scalar in entry.findall('scalar'):         scalarnames = scalar.findall("./displayname")         scalarnamestext = scalar.find("./displayname").text         tabscalar = np.append(tabscalar,scalarnamestext)         tab = np.append(tab,(entrynamestext,scalarnamestext))  print(tab) 

but outputs me this

['assembly.iam' 'd0 (value = 0 mm)' 'assembly.iam' 'd1 (value = 0 mm)' 'ground.ipt' 'ground_length (value = 160 mm)'  'ground.ipt' 'd2 (value = 80 mm)'] 

to wanted structure, have build lists of lists:

import os xml.etree import celementtree  workingdir = "c:\\users\\vince\\test" tree = celementtree.parse(os.path.join(newstrworkingdir, "test.xml")) root = tree.getroot() tab = []  entry in root.findall('entry'):     entry_name = entry.findtext("./displayname")     scalar_names = [e.text e in entry.findall('scalar/displayname')]     tab.append((entry_name, scalar_names)) print(tab) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -