【Medical Imaging Data Processing】 XML file format processing summary

xml(Extensible Markup Language,简称:XML)Extensible Markup Language is anetconf configuration file format. It is an inherently hierarchical data format, and the most natural representation is to parse it into a tree. However, xmlsuch label data is geared towards “machine” recognition and is not conducive to “human” recognition , so it seems to be more troublesome.

The Netconf working group was established in May 2003. The working group is mainly to propose a new XML-based network configuration (NETCONF) protocol And established.

PythonBuilt-in API:xml.etree.ElementTree can parsexml files. ElementTreeParses the entire xml document into a tree structure, Element represents a single node in this tree structure.

Government document site: The ElementTree XML API

The following example is in the form of the following xml file, which is also an official case. xmlThe document examples are as follows:

xmlDocument examples are as follows:

<?xml version="1.0"?>
<data id="world country">
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

1. xml data format

ElementThe format corresponding to common attributesxml is as follows:

<tag attrib=“netmiko_inventory”>text</tag>

example:

<device_type desc="platform">cisco_ios</device_type>
    tag       attrib         text       tag

in:

1, tag: It is a str object, representing xml tags. In the example, it is closed before and after. device_type
2. attrib: It is a dict object, representing xml attributes, in the example desc="platform"
3. text: It is the content wrapped in the xml data tag, also Element content, in the example cisco_ios
4, child elements: it is the subset included in a pair of tags xml, As shown below, it is similar to the content wrapped in the country tag

Example:

import xml.etree.ElementTree as ET

# 读取、解析文件,获取跟元素
tree = ET.parse('sample.xml')
root = tree.getroot()

# 获取根元素的标签名称以及所有属性
# data
print('root.tag:', root.tag)
print('root.attrib:', root.attrib)

# 获取text
print(root[0][1].text)

Output result:

root.tag: data
root.attrib: {'id': 'world country'}
2008

2. Search method

ElementThere are a lot of search methods, summarized as follows:

  • iter(tag=None): ItineraryElementtargetchild, possibletagspecified;
  • findall(match): Find the node that the current element tag or path can match; child
  • find(match): Find the first node that the current element tag or path can match; child
  • get(key, default=None): Specify the element to be takenkeypursuantattrib, be fruitlessattrib, returndefault.
# 一次性获取所有名为 country 的元素、然后遍历
for country in root.findall('country'):
    # 获取 country 元素中的 name 属性
    name = country.get('name')

    # 寻找名为 rank 的子节点,获取其text
    rank = country.find('rank').text

    # 一次性获取所有名为 country 下 neighbor的元素、然后遍历
    neighbors = country.findall('neighbor')

    neighbor_name = []
    for neighbor in neighbors:
        n = neighbor.get('name')
        neighbor_name.append(n)
    print(name, rank, neighbor_name)

The print result is as follows:

Liechtenstein 1 ['Austria', 'Switzerland']
Singapore 4 ['Malaysia']
Panama 68 ['Costa Rica', 'Colombia']

3. iter iterator

Element uses the iter iterator to recursively traverse all children below it.

# 一次性获取所有名为 country 的元素、然后遍历
for country in root.iter('country'):
    # 获取 country 元素中的 name 属性
    name = country.get('name')
    print(name)

for rank in root.iter('rank'):
    # 获取 country 元素中的 name 属性
    rank_n = rank.text
    print(rank_n)

The print result is as follows:

Liechtenstein
Singapore
Panama

1
4
68

4. Convert xml data into dictionary

xmltodict is very simple to use and has low learning cost. It can quickly convert data in XML format into a dictionary and easily perform secondary data processing.

code show as below:

import xmltodict, json

with open('sample.xml') as f:
    my_dict = xmltodict.parse(f.read())
    
with open('info1.json', 'w') as f:
    json.dump(my_dict, f, sort_keys=False, indent=2)

Openinfo1.json to view, the saved content is as follows:

{
  "data": {
    "@id": "world country",
    "country": [
      {
        "@name": "Liechtenstein",
        "rank": "1",
        "year": "2008",
        "gdppc": "141100",
        "neighbor": [
          {
            "@name": "Austria",
            "@direction": "E"
          },
          {
            "@name": "Switzerland",
            "@direction": "W"
          }
        ]
      },
      {
        "@name": "Singapore",
        "rank": "4",
        "year": "2011",
        "gdppc": "59900",
        "neighbor": {
          "@name": "Malaysia",
          "@direction": "N"
        }
      },
      {
        "@name": "Panama",
        "rank": "68",
        "year": "2011",
        "gdppc": "13600",
        "neighbor": [
          {
            "@name": "Costa Rica",
            "@direction": "W"
          },
          {
            "@name": "Colombia",
            "@direction": "E"
          }
        ]
      }
    ]
  }
}

in:

  • tag and attrib will become dictionary key, for example, the above data、id、country、name、rank and so on;
  • However, with attributes attrib will automatically add the symbol, while others will not;key a>@
  • key corresponds to value, which will become a string.

5. Save as xml

xmltodict can not only convert xml into dictionary form, but also convert the dictionary into xml form and store it locally, as shown below:

def saveXML():
    import xmltodict

    with open('info.xml', 'w') as f:
           info_dict = {
    
    
                   'dev_info': {
    
    
                           'device_type': 'cisco_ios',
                           'username': 'admin',
                           'password': 'cisco',
                           'ip': '192.168.47.10'
                  }
          }
           f.write(xmltodict.unparse(info_dict, pretty=True))

After saving it as an xml file, open it and view the results as follows:

<?xml version="1.0" encoding="utf-8"?>
<dev_info>
	<device_type>cisco_ios</device_type>
	<username>admin</username>
	<password>cisco</password>
	<ip>192.168.47.10</ip>
</dev_info>

6. Summary

I briefly studied the xml file before, including what xml is and how to use it in python Read, manipulate and store asxmlfiles. At the same time, machine-friendlyxml files are converted into human-friendly dictionary viewing. xmlIt is indeed not easy to view and analyze, and it is a bit laborious to do.

Guess you like

Origin blog.csdn.net/wsLJQian/article/details/134058924