I have the following XML structure
<root>
<OuterLevel>
<Node>
<Name>NodeA</Name>
</Node>
<Node>
<Name>NodeB</Name>
<Node>
<SpecialNode>
<Name>NodeZ</Name>
</SpecialNode>
</OuterLevel>
</root>
I did some reading on python’s ElementTree XML API and I wanted to compare an element’s tag with a string. According to the document found on http://docs.python.org/library/xml.etree.elementtree.html, the tag of an element is a string. I wrote the following python code to test for equality:
import xml.etree.ElementTree as ET
tree = ET.parse('sampleFile.xml')
root = tree.getroot()
if root[0][0].tag != 'Node'
print("not equal")
However, when I ran the python code. I keep getting SyntaxError: invalid syntax with a carrot sign pointing at the ‘ after Node. I can print the results of root[0][0].tag Is it not possible to compare that with a string?
You need a colon after the
ifstatement. E.g:A syntax error means there was an error when trying to understand your code – not while trying to run it. It’s worth noting as it makes it easier to find the root cause of the problem.