G
G
gadzhi152016-04-13 14:01:47
Python
gadzhi15, 2016-04-13 14:01:47

Parsing XML with Python. How to solve the problem in LXML?

There is this code:

file_events = "1.xml"
f = open(file_events)
xml = f.read()
f.close()
tree = etree.parse(StringIO(xml))

The file_events variable contains the name of the file to be parsed. It is located in the folder with the script. When compiling, it shows an error.
tree = etree.parse(StringIO(xml))
  File "lxml.etree.pyx", line 3427, in lxml.etree.parse (src\lxml\lxml.etree.c:79720)
  File "parser.pxi", line 1799, in lxml.etree._parseDocument (src\lxml\lxml.etree.c:116138)
  File "parser.pxi", line 1819, in lxml.etree._parseMemoryDocument (src\lxml\lxml.etree.c:116413)
  File "parser.pxi", line 1707, in lxml.etree._parseDoc (src\lxml\lxml.etree.c:115063)
  File "parser.pxi", line 1079, in lxml.etree._BaseParser._parseDoc (src\lxml\lxml.etree.c:109462)
  File "parser.pxi", line 573, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:103323)
  File "parser.pxi", line 683, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:104977)
  File "parser.pxi", line 613, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:103886)
lxml.etree.XMLSyntaxError: Premature end of data in tag event line 784, line 786, column 7

But if you specify like this:
tree = etree.parse('1.xml')
Then everything works.
What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2016-04-13
@fox_12

Try like this:

....
your_data = StringIO.StringIO()
your_data.write(xml)
your_data.seek(0)
tree = etree.parse(your_data)

You have a pointer at the end of the file when you write to StringIO ZY
.
In general, it is better to open the file like this:
Either get rid of the extra link altogether
tree = etree.parse(file_events)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question