读取文件“/media/xml/example.xml”时出错:加载外部实体“/media/xml/example.xml”失败
Error reading file '/media/xml/example.xml': failed to load external entity "/media/xml/example.xml"
在我的 Django 项目中,我尝试加载外部 xml 文件并在其中查找一些数据。
但是我收到一条错误消息。
Error reading file '/media/xml/example.xml': failed to load external entity "/media/xml/example.xml"
这是我的代码:
xml_file = etree.parse('/media/xml/example.xml')
find_data = etree.XPath("Some text")
data_result = find_data(xml_file)
你能帮我吗?谢谢。
推荐给大家Beautifulsoup,用起来很爽很方便:
dir = '/'.join([settings.BASE_DIR, 'media', 'xml/example.xml'])
abs_path = os.path.realpath(dir)
soup = BeautifulSoup(open(abs_path)) #<-- here you can now read/search thru xml file
for row in soup.find_all('row'):
print row.find('name').text
如果您的 xml 看起来像:
<?xml version='1.0' encoding='us-ascii'?>
<root>
<row>
<name>Wake up to BeautifulSoup!</name>
</row>
</root>
您将获得:
Wake up to BeautifulSoup!
文档是 here
在我的 Django 项目中,我尝试加载外部 xml 文件并在其中查找一些数据。 但是我收到一条错误消息。
Error reading file '/media/xml/example.xml': failed to load external entity "/media/xml/example.xml"
这是我的代码:
xml_file = etree.parse('/media/xml/example.xml')
find_data = etree.XPath("Some text")
data_result = find_data(xml_file)
你能帮我吗?谢谢。
推荐给大家Beautifulsoup,用起来很爽很方便:
dir = '/'.join([settings.BASE_DIR, 'media', 'xml/example.xml'])
abs_path = os.path.realpath(dir)
soup = BeautifulSoup(open(abs_path)) #<-- here you can now read/search thru xml file
for row in soup.find_all('row'):
print row.find('name').text
如果您的 xml 看起来像:
<?xml version='1.0' encoding='us-ascii'?>
<root>
<row>
<name>Wake up to BeautifulSoup!</name>
</row>
</root>
您将获得:
Wake up to BeautifulSoup!
文档是 here