首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python解析同名元素的xml数据

如何使用python解析同名元素的xml数据
EN

Stack Overflow用户
提问于 2017-06-04 20:38:19
回答 1查看 715关注 0票数 0

嗨,我有这个xml,我想得到与每个分支元素的名称和类型相关的字符串和FullProductName字符串元素(“(Management ) 3.0(1)c")。我正在尝试使用bs4来处理python。但我不知道该怎么做。

XML:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<cvrfdoc xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.icasi.org/CVRF/schema/cvrf/1.1">
  <DocumentTitle>Cisco Integrated Management Controller Remote Code Execution Vulnerability</DocumentTitle>
  <DocumentType>Cisco Security Advisory</DocumentType>
  <DocumentPublisher Type="Vendor">
    <ContactDetails>Emergency Support:
+1 877 228 7302 (toll-free within North America)
+1 408 525 6532 (International direct-dial)
Non-emergency Support:
Email: psirt@cisco.com
Support requests that are received via e-mail are typically acknowledged within 48 hours.</ContactDetails>
    <IssuingAuthority>Cisco product security incident response is the responsibility of the Cisco Product Security Incident Response Team (PSIRT). The Cisco PSIRT is a dedicated, global team that manages the receipt, investigation, and public reporting of security vulnerability information that is related to Cisco products and networks. The on-call Cisco PSIRT works 24x7 with Cisco customers, independent security researchers, consultants, industry organizations, and other vendors to identify possible security issues with Cisco products and networks.
More information can be found in Cisco Security Vulnerability Policy available at http://www.cisco.com/web/about/security/psirt/security_vulnerability_policy.html</IssuingAuthority>
  </DocumentPublisher>
  <DocumentTracking>
    <Identification>
      <ID>cisco-sa-20170419-cimc3</ID>
    </Identification>
    <Status>Final</Status>
    <Version>1.2</Version>
    <RevisionHistory>
      <Revision>
        <Number>1.0</Number>
        <Date>2017-04-18T16:50:37</Date>
        <Description>Initial public release.</Description>
      </Revision>
      <Revision>
        <Number>1.1</Number>
        <Date>2017-05-22T17:55:14</Date>
        <Description>Updated affected products.</Description>
      </Revision>
      <Revision>
        <Number>1.2</Number>
        <Date>2017-05-31T20:33:19</Date>
        <Description>Added vulnerable releases.</Description>
      </Revision>
    </RevisionHistory>
    <InitialReleaseDate>2017-04-19T16:00:00</InitialReleaseDate>
    <CurrentReleaseDate>2017-05-31T20:33:19</CurrentReleaseDate>
    <Generator>
      <Engine>TVCE</Engine>
    </Generator>
  </DocumentTracking>
  <DocumentNotes>
    <Note Title="Summary" Type="General" Ordinal="1">A vulnerability in the web-based GUI of Cisco Integrated Management Controller (IMC) could allow an unauthenticated, remote attacker to perform unauthorized remote command execution on the affected device.

The vulnerability exists because the affected software does not sufficiently sanitize specific values that are received as part of a user-supplied HTTP request. An attacker could exploit this vulnerability by sending a crafted HTTP request to the affected software. Successful exploitation could allow an unauthenticated attacker to execute system commands with root-level privileges.

There are no workarounds that address this vulnerability.

This advisory is available at the following link:
https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20170419-cimc3 ["https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20170419-cimc3"]</Note>
    <Note Title="CVSS 3.0 Notice" Type="Other" Ordinal="2">Although CVRF version 1.1 does not support CVSS version 3, the CVSS score in this CVRF file is a CVSSv3 base and temporal score, as Cisco is now scoring vulnerabilities in CVSSv3.</Note>
  </DocumentNotes>
  <DocumentReferences>
    <Reference Type="Self">
      <URL>https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20170419-cimc3</URL>
      <Description>Cisco Integrated Management Controller Remote Code Execution Vulnerability</Description>
    </Reference>
  </DocumentReferences>
  <ProductTree xmlns="http://www.icasi.org/CVRF/schema/prod/1.1">
    <Branch Name="Cisco" Type="Vendor">
      <Branch Name="Cisco Unified Computing System (Management Software)" Type="Product Name">
        <Branch Name="3.0" Type="Product Version">
          <Branch Name="(1)c" Type="Service Pack">
            <FullProductName ProductID="CVRFPID-203522">Cisco Unified Computing System (Management Software) 3.0(1)c</FullProductName>
          </Branch>
        </Branch>
      </Branch>
    </Branch>
  </ProductTree>
  <Vulnerability Ordinal="1" xmlns="http://www.icasi.org/CVRF/schema/vuln/1.1">
    <Title>Cisco Integrated Management Controller Remote Code Execution Vulnerability</Title>
    <ID SystemName="Cisco Bug ID">CSCvd14578</ID>
    <Notes>
      <Note Title="Summary" Type="Summary" Ordinal="1">A vulnerability in the web-based GUI of Cisco Integrated Management Controller (IMC) could allow an unauthenticated, remote attacker to perform unauthorized remote command execution on the affected device.



The vulnerability exists because the affected software does not sufficiently sanitize specific values that are received as part of a user-supplied HTTP request. An attacker could exploit this vulnerability by sending a crafted HTTP request to the affected software. Successful exploitation could allow an unauthenticated attacker to execute system commands with root-level privileges.</Note>
      <Note Title="Cisco Bug IDs" Type="Other" Ordinal="3">CSCvd14578</Note>
    </Notes>
    <CVE>CVE-2017-6616</CVE>
    <ProductStatuses>
      <Status Type="Known Affected">
        <ProductID>CVRFPID-203522</ProductID>
      </Status>
    </ProductStatuses>
    <CVSSScoreSets>
      <ScoreSet>
        <BaseScore>9.8</BaseScore>
        <Vector>CVSS:3.0/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H</Vector>
      </ScoreSet>
    </CVSSScoreSets>
    <Remediations>
      <Remediation Type="Workaround">
        <Description>There are no workarounds that address this vulnerability.</Description>
      </Remediation>
    </Remediations>
    <References>
      <Reference Type="Self">
        <URL>https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-20170419-cimc3</URL>
        <Description>Cisco Integrated Management Controller Remote Code Execution Vulnerability</Description>
      </Reference>
    </References>
  </Vulnerability>
</cvrfdoc>

Python:

代码语言:javascript
复制
from bs4 import BeautifulSoup

xmlData = open("test.xml")
soup = BeautifulSoup(xmlData, "lxml")
preoductTree = soup.producttree

vendor = preoductTree.find_all("branch", attrs={"type": "Vendor"})

有什么想法吗?

提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-04 21:02:04

因此,您必须首先找到标记,这样我们才能遍历文件。我将使用固有的python xml包。

代码语言:javascript
复制
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()

然后,让我们找到第一个子标记,以便我们可以运行文件。

代码语言:javascript
复制
for child in root:
    print child.tag, child.attrib

>>{http://www.icasi.org/CVRF/schema/prod/1.1}Branch, {'Type': 'Vendor', 'Name': 'Cisco'}

因此,正如您所看到的,它不仅仅是您指定的branch

现在,我们可以递归地遍历整个文件并获取所有这些元素。由于您没有指定所需的输出结构,所以我将把它放在字典中。

代码语言:javascript
复制
val_dict = dict()
for schild in root.iter('{http://www.icasi.org/CVRF/schema/prod/1.1}Branch'):
        val = schild.attrib
        val_dict[val.get('Type')] = val.get('Name')

>>{'Product Name': 'Cisco Unified Computing System (Management Software)',
 'Product Version': '3.0',
 'Service Pack': '(1)c',
 'Vendor': 'Cisco'}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44358551

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档