Python: Append two XML tags -


i have xml file , i'd read data out of using python's xml.etree :

<a>    <b>       <authorname>          <givenname>john</givenname>           <familyname>smith</familyname>       </authorname>       <authorname>          <givenname>saint</givenname>           <givenname>patrick</givenname>          <familyname>thomas</familyname>       </authorname>    </b> </a> 

the result wish have :

john smith saint patrick thomas 

the thing, may have noticed, have 1 givenname tag , have 2 givenname tags

what did :

from xml.etree import elementtree et xx = et.parse('file.xml') authorname = xx.findall('.//authorname') name in authorname:     print(name[0].text + " " + name[1].text) 

it works fine 1 givenname tag not when have 2.

what can do?

thanks!

try this:

from xml.etree import elementtree et xx = et.parse('file.xml') authorname = xx.findall('.//authorname') name in authorname:     namestr = ' '.join([child.text child in name])     print(namestr) 

you have @ child tags inside authorname, take text , join them namestr.


Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -