简体   繁体   English

xml.etree.ElementTree.ParseError: unbound prefix: 如何在不对 xml 文件进行任何更改的情况下解决此问题

[英]xml.etree.ElementTree.ParseError: unbound prefix: How to solve this issue without making any change to xml file

This is my sample xml file:这是我的示例 xml 文件:

<?xml version="1.0"?>
<NewSpg>
     <!--Supported Range-->

<ABC data="LS">
   <number Info="0x00" Id="DFS" Bus="0" Device="2" Range="4" Value="0x1A1012f5" no_id="he d kd" /> 
          <number Info="0x32" Id="bhi"  Range="4" Value="0x000012f5" />
          <number Info="0x336" Id="bhi" Range="4" Value="0x00000df2" />
</ABC>
<!--New Info-->
   <NEW_VALUES>
         <Spg:Thing data="First_Thing" Result="true"/>
         <Spg:Thing data="SecondThing" Result="800000000"/>
   </NEW_VALUES>

</NewSpg>

when i try to parse the xml:当我尝试解析 xml 时:

import xml.etree.ElementTree as ET
xml_file = "name.xml"
xml_tree = ET.parse(xml_file)
root = xml_tree.getroot()

Following Error occured:发生以下错误:

xml.etree.ElementTree.ParseError: unbound prefix: line 13, column 9

I have referred many sites but most of them gives only one solution ie to provide namespace.我已经提到了很多网站,但大多数网站只提供了一种解决方案,即提供名称空间。 But I can't add or delete anything in this xml file.但我无法在这个 xml 文件中添加或删除任何内容。 So is there any method by which i can parse this xml file without modifying the file.那么有没有什么方法可以在不修改文件的情况下解析这个xml文件。

I hope, this will fix your issue (use lxml ETREE)我希望,这会解决你的问题(使用 lxml ETREE)

from lxml import etree
xml_file = "./unbound_prefix.xml"
parser1 = etree.XMLParser(encoding='utf-8', recover=True)
xml_tree = etree.parse(xml_file, parser=parser1)
root = xml_tree.getroot()
root

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Python 中解析 XML 文件 - xml.etree.ElementTree.ParseError - Cannot parse XML files in Python - xml.etree.ElementTree.ParseError 尝试使用 PY3 从 XML 中提取数据时出现 xml.etree.ElementTree.ParseError 问题 - xml.etree.ElementTree.ParseError issue when trying to extract data from XML using PY3 'xml.etree.ElementTree.ParseError: no element found' 制作 python class 时 - 'xml.etree.ElementTree.ParseError: no element found' when making python class xml.etree.ElementTree.ParseError:文档元素后出现垃圾 - xml.etree.ElementTree.ParseError: junk after document element xml.etree.ElementTree.ParseError -- 异常处理未捕获错误 - xml.etree.ElementTree.ParseError -- exception handling not catching errors 修复 xml.etree.ElementTree.ParseError: undefined entity è - Fixing xml.etree.ElementTree.ParseError: undefined entity &egrave 为什么我无法读取 XML 示例文件。 xml.etree.ElementTree.ParseError:语法错误:第 1 行,第 0 列 - Why can't I read a XML example file. xml.etree.ElementTree.ParseError: syntax error: line 1, column 0 尝试使用python解析xml时出错:xml.etree.ElementTree.ParseError:语法错误:第1行 - Error trying parsing xml using python : xml.etree.ElementTree.ParseError: syntax error: line 1, xml python prasing 3错误(xml.etree.ElementTree.ParseError:格式不正确(无效令牌):第1行,第2列) - xml prasing in python 3 errors (xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 2) xml.etree.ElementTree.ParseError:格式不正确(无效令牌):第104行,第109列 - xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 104, column 109
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM