【发布时间】:2017-12-15 08:09:29
【问题描述】:
我需要同时解析 var 和 group 根元素。
代码
import xml.etree.ElementTree as ET
tree_ownCloud = ET.parse('0020-syslog_rules.xml')
root = tree_ownCloud.getroot()
错误
xml.etree.ElementTree.ParseError:文档元素后的垃圾:第 17 行,第 0 列
示例 XML
<var name="BAD_WORDS">core_dumped|failure|error|attack| bad |illegal |denied|refused|unauthorized|fatal|failed|Segmentation Fault|Corrupted</var>
<group name="syslog,errors,">
<rule id="1001" level="2">
<match>^Couldn't open /etc/securetty</match>
<description>File missing. Root access unrestricted.</description>
<group>pci_dss_10.2.4,gpg13_4.1,</group>
</rule>
<rule id="1002" level="2">
<match>$BAD_WORDS</match>
<options>alert_by_email</options>
<description>Unknown problem somewhere in the system.</description>
<group>gpg13_4.3,</group>
</rule>
</group>
我尝试在 stackoverflow here 上关注其他几个问题,但没有任何帮助。
我知道原因,由于它没有被解析,人们通常会尝试破解。 IMO 在 XML 中有多个根元素是一个非常常见的用例,ET 解析库中必须有一些东西才能完成。
【问题讨论】:
-
好吧。 “IMO 在 XML 中有多个根元素是一个非常常见的用例,” - 这是不正确的。根据 XML 的定义,它总是只有一个根元素。
-
好的,不知道谢谢
标签: python xml python-2.7 parsing