python - ElementTree findtext doesn't find leaf nodes -


I'm using Elementary to scan the XML string drawn from a text file.

  & lt; Root & gt; & Lt; Branch_a & gt; & Lt; Leaf & gt; Foo & lt; / Leaf & gt; & Lt; / Branch_a & gt; & Lt; Branch_b & gt; & Lt; Another_leaf & gt; Bar & lt; / Another_leaf & gt; & Lt; / Branch_b> & Lt; / Root & gt;  

When I parse it and try to find the nodes of the leaf, I do not get any results:

  gt; & Gt; Elem_tree = xml.etree.ElementTree.fromstring (xml_string)> gt; & Gt; & Gt; Leaf_text = elem_tree.findtext ('leaf') & gt; & Gt; & Gt; Leaf_text is not true  

But when I cross the tree, everything works fine:

  gt; & Gt; Elem_tree.findtext ('branch_a / leaf') 'foo' & gt; & Gt; & Gt; Branch = elem_tree.find ('branch_a') & gt; & Gt; & Gt; Branch.findtext ('leaf') 'foo'  

Is there a way for Elementary to scan the whole tree for me? The name of my branch is dynamic, the address which I see can remain in any branch.

I was able to do it with minidom, but ran in other boundaries for reference (error hidden investigation) here I am doing this.

  & gt; & Gt; & Gt; Xml_doc = xml.dom.minidom.parseString (xml_string)> gt; & Gt; & Gt; Leaf_node = xml_doc.getElementsByTagName ('leaf') & gt; & Gt; & Gt; Leaf_node [0] .firstChild.nodeValue 'foo'  

search text < / Code> Elementitory style uses the path statement, so you were only scanning the children immediately, it will scan the entire tree (see):

  leaf_text = elem_tree.findtext ('. 5 leaf ')  

Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -