SoFunction
Updated on 2024-11-21

Conversion methods for xml format in python

The conversion of xml format in python for your reference is as follows

Recently in the project when you need to do the data for xml and dict conversion, here is a preliminary summary

1. Based on DOM.

write

# Import
import  as minidom
# Create document instances
dom = ().createDocument(None,'Root',None)
# Getting the root node
root = 

for i in range(5):
    # Create nodes
    element = ('Name')
    # Add data to this node
    (('default'))
    # Setting Properties
    ('age', str(i))
    # Add to node
    (element)
# Save the file Auto indentation relative to the original write back
with open('', 'w', encoding='utf-8') as f:
    (f, addindent='\t', newl='\n',encoding='utf-8')

# Documentation
<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Name age="0">default</Name>
    <Name age="1">default</Name>
    <Name age="2">default</Name>
    <Name age="3">default</Name>
    <Name age="4">default</Name>
</Root>

# ==================================
If it is necessary to change the already writtenxmlIndentation of documents
The following code can be executed  root is the root node of the acquisition
import  as ET
from  import minidom

def save_xml(root, filename, indent="\t", newl="\n", encoding="utf-8"):
    raw_text = (root)
    dom = (raw_Text)
    with open(filename, "w") as f:
        (f, indent, newl, encoding)  

retrieve

import  as ET
from  import minidom

# Read the document
dom = ("")
# Get the root node
root = 
# Find word nodes by name, notice here # # Recursively find all children # # All children.
names = ("Name")
for name in names:
    
    print([0].nodeValue, end="\t")
    # Query whether name contains the attribute age
    if ("age"):
        # Outputs looking at attributes age
        print(("age"), end="\t")
    print("")

2. Based on ElementTree

write

# Import
# -*- coding:utf-8 -*-
import  as ET
# Add line breaks
def __indent(elem, level=0):
    i = "\n" + level*"\t"
    if len(elem):
        if not  or not ():
             = i + "\t"
        if not  or not ():
             = i
        for elem in elem:
            __indent(elem, level+1)
        if not  or not ():
             = i
    else:
        if level and (not  or not ()):
             = i

root = ('Root')       # Create nodes
tree = (root)     # Create Documentation

for i in range(5):
    element = ('Name')
    ('age', str(i))
     = 'default'
    (element)

__indent(root)          # Add line breaks
('', encoding='utf-8', xml_declaration=True)

# Documentation
<?xml version='1.0' encoding='utf-8'?>
<Root>
    <Name age="0">default</Name>
    <Name age="1">default</Name>
    <Name age="2">default</Name>
    <Name age="3">default</Name>
    <Name age="4">default</Name>
</Root>

retrieve

# -*- coding:utf-8 -*-

import  as ET

# Get Documentation
tree = ('')
# Get the root node
root = ()
# Get all child nodes list(root)
# Find all children (non-recursive) ("Name") Recursive ("Name")
for node in list(root):
    print(, , ('age'))

for node in ('Name'):
    print(, , ('age'))

# Output
default Name 0
default Name 1
default Name 2
default Name 3
default Name 4
default Name 0
default Name 1
default Name 2
default Name 3
default Name 4

Using modules such as dicttoxml xmltodict

1, parsing xml files.

Use iterfind to find nodes, get child nodes method list(node), get node attribute get(attribute name), value of next level node findtext

from  import parse
try:
  doc=parse('')
  for item in ('class'):
      classname=('a_name')
      print("classname=",classname)
      for s in list(item):
        name=('name')
        age = ('age')
        sex = ('sex')
        print("name=",name,"age=",age,"sex=",sex)
      print("-------------------")
except Exception as e:
  print(e)

2. Dictionary converted to xml file.

Use dicttoxml module, method: (dictionary data, root node name custom_root=") import dicttoxml

from  import parseString
import os
d=[20,'name',
  {'name':'apple','num':10,'price':23},
  {'name': 'pear', 'num': 20, 'price': 18.7},
  {'name': 'banana', 'num': 10.5, 'price': 23}]
bxml=(d,custom_root='fruit')
xml=('utf-8')
print(xml)
dom=parseString(xml)
pxml=(indent='  ')
f=open('','w',encoding='utf-8')
(pxml)
()

3. xml file to dictionary.

Use xmltodict module , method: (xml string)

import xmltodict
import pprint
f=open('')
xml=()
d=(xml)
pp=(indent=4)
(d)#Can access text values in nodes via d['root']['arg']['#text'], and attribute values via d['root']['arg']['@p']
()

4, the dictionary is converted to json

Using the dumps method of json

import json
data={'name':'bill','company':'huawei','age':30}
jsonstr=(data)
print(jsonstr)

5. json converted to dictionary:

Use the loads function of the json module to pass in a json string and return the dictionary corresponding to that string.

d=(jsonstr) print(d)

6, json conversion to class instances

1), in the specified class must have a constructor that accepts dictionaries; or specify the callback function json2Product;

2), use json's loads method (json string, object_hook=class name or callback function name)

import json
class Product:
  def __init__(self,d):
    self.__dict__=d
def json2Product(d):
  return Product(d)
f=open('','r',encoding='utf-8')
strjson=()
products=(strjson,object_hook=Product)
for p in products:
  print('name=',,'price=',)

7. Class instances are converted to json:

1), specify the callback function (product2Dict)

2), the use of json dump function, specify the default parameters of the callback function import json

def product2Dict(product):
  return {
    'name': ,
    'price': ,
    'count': 
    }
strJson=(products,default=product2Dict)
print(strJson)

8. Dictionaries are converted to classes:

1), the dictionary will be converted to json

2), json conversion to class

import json
data=[{"name": "iPhone9", "price": 9999, "count": 3000}, {"name": "tesila", "price": 800000, "count": 122}]
# Convert dictionary to json
jsonstr=(data)
class Product:
  def __init__(self,d):
    self.__dict__=d
def json2Product(d):
  return Product(d)
# Convert json to class
ps=(jsonstr,object_hook=Product)
for p in ps:
  print('name=', , 'price=', )

9. Convert the class to a dictionary:

1), the class is converted to json, using json dumps method

2), json to dictionary, using json loads method

def product2Dict(product):
  return {
    'name': ,
    'price': ,
    'count': 
    }
# Convert classes to json
strJson=(ps,default=product2Dict)
print(strJson)
d=(strJson)
print(d)

10, json to xml

1), first convert xml to dictionary

2), then use dicttoxml to convert to dictionary

import json
import dicttoxml
f=open('','r',encoding='utf-8')
jsonstr=()
# Convert json to dictionary
d=(jsonstr)
print(d)
# Convert dictionary to xml
bxml=(d,custom_root='fruit')
print(bxml)

11, the xml will be converted to json

1), first use xmltodict conversion to dictionary

2), and then convert the dictionary to json

import xmltodict
import json
f=open('','r',encoding='utf-8')
d=()
# Convert xml to dictionary first
data=(d)
print(data)
# then convert the dictionary to json
strjson=(data)
print(strjson)

This is the whole content of this article.