在python中,只需尝试解析XML:
import xml.etree.ElementTree as ET
data = 'info.xml'
tree = ET.fromstring(data)
但得到了错误:
Traceback (most recent call last):
File "C:\mesh\try1.py", line 3, in <module>
tree = ET.fromstring(data)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1312, in XML
return parser.close()
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1665, in close
self._raiseerror(v)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1517, in _raiseerror
raise err
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0
那是一些xml,我有:
<?xml version="1.0" encoding="utf-16"?>
<AnalysisData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<BlendOperations OperationNumber="1">
<ComponentQuality>
<MaterialName>Oil</MaterialName>
<Weight>1067.843017578125</Weight>
<WeightPercent>31.545017776585109</WeightPercent>
为什么会这样?
答案 0 :(得分:7)
您尝试解析字符串'info.xml'
而不是文件内容。
您可以拨打tree = ET.parse('info.xml')
来打开该文件。
或者您可以直接阅读该文件:
ET.fromstring(open('info.xml').read())