我尝试使用lxml解析XML文件。它适用于一些文件,但最后一个文件给了我一个错误:
XPathEvalError at /admin/xml_exportation/xml/add/
Invalid expression
这是xml代码:
<?xml version="1.0" encoding="utf-8" ?>
<export source="Source" version="1.0" date="2014-02-21T17:46:57">
<Agence ID="XX1" externRef="XX1" customCode="38495">
<Biens>
<Bien ID="XX1-176" ref="XX1-176">
<DateMAJ>2013-06-14T12:12:07</DateMAJ>
</Bien>
</Biens>
</Agence>
</export>
然后是python代码:
tree = etree.parse(xml_file)
root_path = '/export/Agence/Biens/Bien/'
for element in tree.xpath(root_path):
print(element)
我试图改变路径,但它给了我同样的错误。
感谢您的帮助。
Internal Server Error: /admin/xml_exportation/xml/add/
Traceback (most recent call last):
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 372, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/views/decorators/cache.py", line 89, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 202, in inner
return view(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 25, in _wrapper
return bound_func(*args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/utils/decorators.py", line 21, in bound_func
return func(self, *args2, **kwargs2)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/db/transaction.py", line 223, in inner
return func(*args, **kwargs)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 1007, in add_view
self.save_model(request, new_object, form, False)
File "/home/python-envs/website.com/local/lib/python2.7/site-packages/django/contrib/admin/options.py", line 740, in save_model
obj.save()
File "astucesexperts/apps/xml_exportation/models.py", line 154, in save
for element in tree.xpath(root_path): # Find each new property
File "lxml.etree.pyx", line 2115, in lxml.etree._ElementTree.xpath (src/lxml/lxml.etree.c:57669)
File "xpath.pxi", line 370, in lxml.etree.XPathDocumentEvaluator.__call__ (src/lxml/lxml.etree.c:146579)
File "xpath.pxi", line 238, in lxml.etree._XPathEvaluatorBase._handle_result (src/lxml/lxml.etree.c:144977)
File "xpath.pxi", line 224, in lxml.etree._XPathEvaluatorBase._raise_eval_error (src/lxml/lxml.etree.c:144832)
XPathEvalError: Invalid expression
我还添加了可以使用脚本导出的xml文件中的代码(更改根路径)。
<?xml version='1.0' encoding='UTF-8' ?>
<ANNONCES>
<ANNONCE>
<AGENCE_REF><![CDATA[22]]></AGENCE_REF>
<AGENCE_NOM><![CDATA[Agence]]></AGENCE_NOM>
<REFERENCE><![CDATA[100lm]]></REFERENCE>
<TRANSACTION><![CDATA[2]]></TRANSACTION>
<TYPE><![CDATA[2]]></TYPE>
<SOUSTYPE><![CDATA[45]]></SOUSTYPE>
<SURFACE><![CDATA[100]]></SURFACE>
<ANNONCE>
</ANNONCES>
答案 0 :(得分:1)
你的XPath有一个尾随'/'。删除它,一切都按预期工作。
即改变:
root_path = '/export/Agence/Biens/Bien/'
到
root_path = '/export/Agence/Biens/Bien'
答案 1 :(得分:0)
<Biens>
的结束标记没有反斜杠,因此您的树是空的,因为XML没有解析。
在尝试使用Python解析之前验证XML。