Home>

demandIn practical applications,Need to modify the xml configuration file in real time,

1.Add, delete some nodes

2.Add, delete, modify some attributes under a node

3.Add, delete, modify the text of some nodes

Use xml document

<?xml version="1.0" encoding="utf-8"?>
<framework>
  <processers>
    <processer name="aprocesser" file="lib64/a.so"
      path="/tmp">
    </processer>
    <processer name="bprocesser" file="lib64/b.so" value="fordelete">
    </processer>
    <processer name="bprocesser" file="lib64/b.so2222222" />
    <services>
      <service name="search" prefix="/bin/search?"
        output_formatter="outputformatter:service_inc">
        <chain sequency="chain1" />
        <chain sequency="chain2"></chain>
      </service>
      <service name="update" prefix="/bin/update?">
        <chain sequency="chain3" value="fordelete" />
      </service>
    </services>
  </processers>
</framework>

Realize thoughtUsing elementtree, read the file first,Parse into a tree,After that, according to the path,Can locate every node of the tree,Modify the node,And finally output it directly

Implementation code

#!/usr/bin/python
#-*-coding=utf-8-*-
#author:[email protected]
#date:2012-05-25
#version:0.1
from xml.etree.elementtree import elementtree, element
def read_xml (in_path):
  "" "Read and parse the xml file
    in_path:xml path
    return:elementtree "" "
  tree=elementtree ()
  tree.parse (in_path)
  return tree
def write_xml (tree, out_path):
  "" "Write the xml file
    tree:xml tree
    out_path:Write out the path "" "
  tree.write (out_path, encoding="utf-8", xml_declaration=true)
def if_match (node, kv_map):
  "" "Judging whether a node contains all incoming parameter attributes
    node:node
    kv_map:map of attributes and attribute values ​​"" "
  for key in kv_map:
    if node.get (key)!=kv_map.get (key):
      return false
  return true
#--------------- search -----
def find_nodes (tree, path):
  "" "Find all nodes matching a certain path
    tree:xml tree
    path:node path "" "
  return tree.findall (path)
def get_node_by_keyvalue (nodelist, kv_map):
  "" "Locates matching nodes based on attributes and attribute values.
Return node
    nodelist:node list
    kv_map:Match attributes and attribute values ​​map "" "
  result_nodes=[]
  for node in nodelist:
    if if_match (node, kv_map):
      result_nodes.append (node)
  return result_nodes
#--------------- change -----
def change_node_properties (nodelist, kv_map, is_delete=false):
  "" "Modify/Add/Delete node attributes and attribute values
    nodelist:node list
    kv_map:attribute and attribute value map "" "
  for node in nodelist:
    for key in kv_map:
      if is_delete:
        if key in node.attrib:
          del node.attrib [key]
      else:
        node.set (key, kv_map.get (key))
def change_node_text (nodelist, text, is_add=false, is_delete=false):
  "" "Change/add/delete a node's text
    nodelist:list of nodes
    text:updated text "" "
  for node in nodelist:
    if is_add:
      node.text +=text
    elif is_delete:
      node.text=""
    else:
      node.text=text
def create_node (tag, property_map, content):
  "" "Create a new node
    tag:node tag
    property_map:map of properties and property values
    content:the text content of the node closing tag
    return new node "" "
  element=element (tag, property_map)
  element.text=content
  return element
def add_child_node (nodelist, element):
  "" "Add child nodes to a node
    nodelist:node list
    element:child node "" "
  for node in nodelist:
    node.append (element)
def del_node_by_tagkeyvalue (nodelist, tag, kv_map):
  "" "Locates a node with attributes and attribute values,And delete it
    nodelist:list of parent nodes
    tag:child node tag
    kv_map:list of attributes and attribute values ​​"" "
  for parent_node in nodelist:
    children=parent_node.getchildren ()
    for child in children:
      if child.tag == tag and if_match (child, kv_map):
        parent_node.remove (child)
if __name__ == "__main__":
  #1. Read the xml file
  tree=read_xml ("./test.xml")
  #2. Attribute modification
   #a. Find the parent node
  nodes=find_nodes (tree, "processers/processer")
   #b. Accurately locate child nodes by attributes
  result_nodes=get_node_by_keyvalue (nodes, {"name":"bprocesser"})
   #c. Modifying node properties
  change_node_properties (result_nodes, {"age":"1"})
   #d. Remove node attributes
  change_node_properties (result_nodes, {"value":""}, true)
  #3. Node modification
   #a. New node
  a=create_node ("person", {"age":"15", "money":"200000"}, "this is the firest content")
   #b. Insert below the parent node
  add_child_node (result_nodes, a)
  #4. Remove nodes
    #Positioning the parent node
  del_parent_nodes=find_nodes (tree, "processers/services/service")
    #Accurately locate child nodes and delete them
  target_del_node=del_node_by_tagkeyvalue (del_parent_nodes, "chain", {"sequency":"chain1"})
  #5. Modifying node text
    #Locating node
  text_nodes=get_node_by_keyvalue (find_nodes (tree, "processers/services/service/chain"), {"sequency":"chain3"})
  change_node_text (text_nodes, "new text")
  #6. Output to the result file
  write_xml (tree, "./out.xml")

Modified result

<?xml version="1.0" encoding="utf-8"?>
<framework>
  <processers>
    <processer file="lib64/a.so" name="aprocesser" path="/tmp">
    </processer>
    <processer age="1" file="lib64/b.so" name="bprocesser">
      <person age="15" money="200000">this is the firest content</person>
    </processer>
    <processer age="1" file="lib64/b.so2222222" name="bprocesser">
      <person age="15" money="200000">this is the firest content</person>
    </processer>
    <services>
      <service name="search" output_formatter="outputformatter:service_inc"
        prefix="/bin/search?">
        <chain sequency="chain2" />
      </service>
      <service name="update" prefix="/bin/update?">
        <chain sequency="chain3" value="fordelete">new text</chain>
      </service>
    </services>
  </processers>
</framework>
  • Previous Follow me about implicit coercion in javascript
  • Next Example to explain the creation of basic animation for iOS application UI development