Gur*_*uru 1 python xml parsing python-2.7
I need to parse both var & group root elements.
Code
import xml.etree.ElementTree as ET
tree_ownCloud = ET.parse('0020-syslog_rules.xml')
root = tree_ownCloud.getroot()
Run Code Online (Sandbox Code Playgroud)
Error
xml.etree.ElementTree.ParseError: junk after document element: line 17, column 0
Sample 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>
Run Code Online (Sandbox Code Playgroud)
I tried following couple of other questions on stackoverflow here, but none helped.
我知道原因,由于它没有得到解析,所以人们通常尝试使用hack。IMO这是一个非常常见的用例,它在XML中具有多个根元素,并且ET解析库中必须存在某些元素才能完成此操作。
如注释中所述,XML文件不能具有多个根。就那么简单。
如果您确实以这种格式接收/存储数据(那么它就是不正确的XML)。您可以考虑用假标签包围您的物品,例如
import xml.etree.ElementTree as ET
with open("0020-syslog_rules.xml", "r") as inputFile:
fileContent = inputFile.read()
root = ET.fromstring("<fake>" + fileContent +"</fake>")
print(root)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1340 次 |
| 最近记录: |