简体   繁体   English

在Python中使用minidom解析XML

[英]Parsing XML using minidom in Python

I'm new to Python and need a little advice on one of my project from the experts. 我是Python的新手,需要专家为我的一个项目提供一些建议。

I have a xml file that i need to parse and then sort . 我有一个XML文件,我需要对其进行分析然后再进行排序。 Below is the example of the xml file 以下是xml文件的示例

<Product_Key Name="Visio Professional 2002" KeyRetrievalNote="">
    <Key ID=“XXX” Type="Static Activation Key">12345-67890</Key>
</Product_Key>


<Product_Key Name="Visio Professional 2008" KeyRetrievalNote="">
    <Key ID=“XXX” Type="Static Activation Key">23456-78901</Key>
</Product_Key>


<Product_Key Name="Visio Professional 2012" KeyRetrievalNote="">
    <Key ID=“XXX” Type="Static Activation Key">34567-89012</Key>
</Product_Key>


<Product_Key Name="Visio Professional 2016” KeyRetrievalNote="">
    <Key ID=“XXX” Type="Static Activation Key">45678-90123</Key>
</Product_Key>

Below is the output that i'm trying to achieve 以下是我正在尝试实现的输出

Visio Professional 2002:   12345-67890
Visio Professional 2008:   23456-78901
Visio Professional 2012:   34567-89012
Visio Professional 2016:   45678-90123

I'm trying to get the name of the product and in front of it the corresponding product key. 我正在尝试获取产品名称,并在其前面获得相应的产品密钥。

I can get the output like below however that is not what I'm looking for. 我可以得到如下输出,但这不是我想要的。

Visio Professional 2002
Visio Professional 2008
Visio Professional 2012
Visio Professional 2016 
12345-67890
23456-78901
34567-89012
45678-90123

The snip of the code that i used is below . 我使用的代码片段如下。

import xml.dom.minidom

def main():
  doc = xml.dom.minidom.parse("keysexport.xml")
  names = doc.getElementsByTagName("Product_Key")
  keys = doc.getElementsByTagName("Key")

  for name in names:
    print(name.getAttribute("Name"))

  for key in keys:
    print(key.firstChild.nodeValue)

if __name__ == "__main__":
  main();

You have done most of the work by yourself. 您已经完成了大部分工作。 Congratulations. 恭喜。

There are many ways to achieve your final goal, one of them is the following: Now that you obtained the names and keys list, you can combine them to construct a dictionary, and then iterate over the dictionary to get the suitable output you are looking for. 实现最终目标的方法有很多,其中一种是以下方法:现在,您已经获得了nameskeys列表,可以将它们组合以构造字典,然后遍历字典以获得所需的输出。对于。

So your program could look like this: 因此您的程序可能如下所示:

import xml.dom.minidom

def main():
  doc = xml.dom.minidom.parse("keysexport.xml")
  names = doc.getElementsByTagName("Product_Key")
  keys = doc.getElementsByTagName("Key")
  #Use the previous lists to create a dictionary
  products = dict(zip(names, keys)) 
  #Loop over the dictionary of products and display the couple key: value
  for product_key, product_value in products.items():
      print('{}:  {}'.format(product_key.getAttribute('Name'), product_value.firstChild.nodeValue))


if __name__ == "__main__":
  main()

Demo: 演示:

>>> names = xmldoc.getElementsByTagName("Product_Key")
>>> keys = xmldoc.getElementsByTagName("Key")
>>> products = dict(zip(names, keys))
>>> for product_key, product_value in products.items():
...     print('{}:  {}'.format(product_key.getAttribute('Name'), product_value.firstChild.nodeValue))
... 
Visio Professional 2002:  12345-67890
Visio Professional 2008:  23456-78901
Visio Professional 2012:  34567-89012
Visio Professional 2016:  45678-90123

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM