1

我正在尝试编写一个 python 脚本,它将打开一个 XML 文件添加一些元素然后退出。我无法让元素树打开我想要的文件。但是,我可以使用 open() 打开文件。示例代码

try:
    file = open(projectLocation + "\etc\config\struts-config.xml", 'r')
    print "oppenned file" #this will print
    tree = ET.parse(projectLocation + "\etc\config\struts-config.xml") #this fails
    print "oppenned xml"
except:
    print "could not open " + projectLocation + "\etc\config\struts-config.xml"
    sys.exit()

将代码更新为此,对于我收到的评论:

    tree = ET.parse(projectLocation + "\etc\config\struts-config.xml")
    print "oppenned xml"

这给了我错误:

        Traceback (most recent call last):
  File "test.py", line 117, in <module>
    tree = ET.parse(projectLocation + "\etc\config\struts-config.xml")
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 862, in parse
    tree.parse(source, parser)
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 586, in parse
    parser.feed(data)
  File "/usr/lib/python2.6/xml/etree/ElementTree.py", line 1245, in feed
    self._parser.Parse(data, 0)
xml.parsers.expat.ExpatError: mismatched tag: line 1205, column 2
4

0 回答 0