Python解析带转义方括号的xml

2024-06-08 01:28:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析一些xml,但它包含一些转义字符。有更简单的方法吗?在

xml:

<?xml version="1.0" encoding="UTF-8"?>
<Group id="RHEL-07-010010">
    <title>SRG-OS-000257-GPOS-00098</title>
    <description>&lt;GroupDescription&gt;&lt;/GroupDescription&gt;    </description>
    <Rule id="RHEL-07-010010_rule" severity="high" weight="10.0">
      <version>RHEL-07-010010</version>
      <title>The file permissions, ownership, and group membership of system files and commands must match the vendor values.</title>
      <description>&lt;VulnDiscussion&gt;Discretionary access control is weakened if a user or group has access permissions to system files and directories greater than the default.

Satisfies: SRG-OS-000257-GPOS-00098, SRG-OS-000278 GPOS-00108&lt;/VulnDiscussion&gt;
   </Rule>
 </Group>

我正在尝试提取description标记中包含的组id、规则严重性、标题和VulnDiscussion。除了VulnDiscussion之外,我可以获取所有内容,因为它包含转义字符>;和<

这是我的代码:

^{pr2}$

崩溃原因:

 newtree = ET.fromstring(unescapedXML)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions /2.7/lib/python2.7/xml/etree/ElementTree.py", line 1300, in XML
    parser.feed(text)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1640, in feed
    self._parser.Parse(data, 0)
TypeError: must be string or read-only buffer, not Element

Tags: andltgtidtitleosversiongroup
1条回答
网友
1楼 · 发布于 2024-06-08 01:28:57

我建议使用^{}而不是标准库的xml,这样会更加健壮和实用。它甚至可以自动取消文本中转义符号的转义。使用XPath也可以让你的生活更轻松。在

from lxml import etree as ET

xml = ET.XML(b"""<?xml version="1.0" encoding="UTF-8"?>
<Group id="RHEL-07-010010">
    <title>SRG-OS-000257-GPOS-00098</title>
    <description>&lt;GroupDescription&gt;&lt;/GroupDescription&gt;    </description>
    <Rule id="RHEL-07-010010_rule" severity="high" weight="10.0">
      <version>RHEL-07-010010</version>
      <title>The file permissions, ownership, and group membership of system files and commands must match the vendor values.</title>
      <description>&lt;VulnDiscussion&gt;Discretionary access control is weakened if a user or group has access permissions to system files and directories greater than the default.

Satisfies: SRG-OS-000257-GPOS-00098, SRG-OS-000278 GPOS-00108&lt;/VulnDiscussion&gt;
      </description>
   </Rule>
 </Group>""")

for description in xml.xpath('//description/text()'):
    vulnDiscussion = next(iter(ET.XML(description).xpath('/VulnDiscussion/text()')), None)
    print(vulnDiscussion)

上面的代码生成

^{pr2}$

相关问题 更多 >

    热门问题