我嘗試使用lxml解析XML文件。它與一些文件,但最後一個給了我一個錯誤:Python lxml/XPathEvalError:解析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>
這是有幫助的,包括回溯,所以我們知道哪一行導致了問題。 – tdelaney 2014-09-30 19:59:53
錯誤意味着xpath表達式'/ admin/xml_exportation/xml/add /'不好 - 尾隨的'/'是非法的。我認爲代碼崩潰在與您展示的地點不同的地方。 – tdelaney 2014-09-30 20:10:15
我用這個代碼與另一個xml文件,它的工作。這就是爲什麼我認爲崩潰來自xml和python。 – pepourquier 2014-10-01 05:03:04