元素树语法错误(使用无效令牌格式不正确)

时间:2017-05-11 06:35:16

标签: python xml python-3.x python-2.x elementtree

我正在尝试使用元素树模块,但我最终遇到了一些我无法理解的错误。

我的代码基于Python文档本身Python Element Tree doc,在尝试运行脚本时它给我一个错误;

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
file_name_xml = "curl-result.xml"
tree = ET.parse(file_name_xml)
tree.getroot()

当我运行此代码时:

./python2.6 modify_xml_file.py    

然后,它给了我这个错误;

Traceback (most recent call last):
  File "modify_xml_file.py", line 8, in <module>
    tree = ET.parse(file_name_xml)
  File "<string>", line 45, in parse
  File "<string>", line 32, in parse
SyntaxError: not well-formed (invalid token): line 1, column 4

1 个答案:

答案 0 :(得分:2)

Python 2.6中包含的cElementTree版本会为格式错误的XML 引发SyntaxError例外:

>>> with open('bad.xml', 'w') as badxml:
...     badxml = '<foobar\n'
...
>>> import xml.etree.cElementTree as ET
>>> tree = ET.parse('bad.xml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 45, in parse
  File "<string>", line 32, in parse
SyntaxError: no element found: line 1, column 0

这是Python 2.7中修复的C加速代码中的错误。 (较慢的)Python解析器会抛出一个更有用的错误:

>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('bad.xml')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse
    tree.parse(source, parser)
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 587, in parse
    self._root = parser.close()
  File "/Users/mjpieters/Development/Library/buildout.python/parts/opt/lib/python2.6/xml/etree/ElementTree.py", line 1254, in close
    self._parser.Parse("", 1) # end of data
xml.parsers.expat.ExpatError: no element found: line 1, column 0

修复XML输入文件。

2.7中的变化是ElementTree是updated to version 1.3improved the parser的版本,引入了新的ParseError异常,它是SyntaxError的子类。