J Jackson
J Jackson

Reputation: 53

Error trying parsing xml using python : xml.etree.ElementTree.ParseError: syntax error: line 1,

In python, simply trying to parse XML:

import xml.etree.ElementTree as ET
data = 'info.xml'
tree = ET.fromstring(data)

but got error:

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

thats a bit of xml, i have:

<?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>

Why is it happening?

Upvotes: 3

Views: 35608

Answers (1)

rkabir
rkabir

Reputation: 588

You're trying to parse the string 'info.xml' instead of the contents of the file.

You could call tree = ET.parse('info.xml') which will open the file.

Or you could read the file directly:

ET.fromstring(open('info.xml').read())

Upvotes: 12

Related Questions