[x3d-public] Can anyone identify the problem with ElementTree or XML when Beautiful Soup succeeds.

vmarchetti at kshell.com vmarchetti at kshell.com
Sun Mar 17 18:26:40 PDT 2019


Thats correct; the find method on an xml.etree.ElementTree.Element instance only search through children.

The search through all descendants is done with the iterfind method, which returns a Python iterator.

I think the code you would use would be something like

list( cn.iterfind("componentInfo") )[0].get("name")

This is brittle code in the sense that it will fail badly if the cn node does not have any componentInfo descendant (a IndexError exception)

Vince Marchetti


> On Mar 17, 2019, at 2:40 AM, John Carlson <yottzumm at gmail.com> wrote:
> 
>  
> import xml.etree.ElementTree                                                    
> from bs4 import BeautifulSoup                                                   
>                                                                                 
> soup = xml.etree.ElementTree.parse(open("../../../../../../specifications/X3dUni
> fiedObjectModel-3.3.xml")).getroot()                                            
> bsoup = BeautifulSoup(open("../../../../../../specifications/X3dUnifiedObjectMod
> el-3.3.xml"), "xml")                                                            
>                                                                                 
> print("BeautifulSoup==========================================================")
> cns = bsoup.find_all("ConcreteNode")                                            
> for cn in cns:                                                                  
>     print(cn["name"]+" component is")                                           
>     print(cn.find("componentInfo")["name"])                                     
>                                                                                 
> print("ElementTree==========================================================")  
> cns = soup.iter("ConcreteNode")                                                 
> for cn in cns:                                                                  
>     print(cn.get("name")+" component is")                                       
>     print(cn.find("componentInfo").get("name"))                                 
> Snip
>  
> ElementTree==========================================================           
> Anchor component is                                                             
> Traceback (most recent call last):                                              
>   File "pythonerror.py", line 17, in                                    
>     print(cn.find("componentInfo").get("name"))                                 
> AttributeError: 'NoneType' object has no attribute 'get'                        
>  
> Apparently find does not find grand children as this code works:
>  
> print(cn.find("InterfaceDefinition").find("componentInfo").get("name"))     
>  
> _______________________________________________
> x3d-public mailing list
> x3d-public at web3d.org <mailto:x3d-public at web3d.org>
> http://web3d.org/mailman/listinfo/x3d-public_web3d.org <http://web3d.org/mailman/listinfo/x3d-public_web3d.org>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20190317/8ce3e57c/attachment-0001.html>


More information about the x3d-public mailing list