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

John Carlson yottzumm at gmail.com
Sat Mar 16 23:40:17 PDT 2019


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"))     
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20190317/1948c9cc/attachment.html>


More information about the x3d-public mailing list