Error trying parsing xml using python : xml.etree.ElementTree.ParseError: syntax error: line 1,
<p>In python, simply trying to parse XML: </p>

<pre><code>import xml.etree.ElementTree as ET
data = 'info.xml'
tree = ET.fromstring(data)
</code></pre>

<p>but got error:</p>

<pre><code>Traceback (most recent call last):
File "C:\mesh\try1.py", line 3, in &lt;module&gt;
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
</code></pre>

<p>thats a bit of xml, i have:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;AnalysisData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
&lt;BlendOperations OperationNumber="1"&gt;
 &lt;ComponentQuality&gt;
  &lt;MaterialName&gt;Oil&lt;/MaterialName&gt;
  &lt;Weight&gt;1067.843017578125&lt;/Weight&gt;
  &lt;WeightPercent&gt;31.545017776585109&lt;/WeightPercent&gt;
</code></pre>

<p>Why is it happening?</p>

<p>You're trying to parse the string <code>'info.xml'</code> instead of the contents of the file.</p>

<p>You could call <code>tree = ET.parse('info.xml')</code> which will open the file.</p>

<p>Or you could read the file directly:</p>

<p><code>ET.fromstring(open('info.xml').read())</code></p>

python xml python-2.7