欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解在Python程序中解析并修改XML內(nèi)容的方法

 更新時(shí)間:2015年11月16日 17:12:11   投稿:goldensun  
這篇文章主要介紹了在Python程序中解析并修改XML內(nèi)容的方法,依賴(lài)于解析成樹(shù)狀結(jié)構(gòu)后的節(jié)點(diǎn)進(jìn)行修改,需要的朋友可以參考下

需求
在實(shí)際應(yīng)用中,需要對(duì)xml配置文件進(jìn)行實(shí)時(shí)修改,

1.增加、刪除 某些節(jié)點(diǎn)

2.增加,刪除,修改某個(gè)節(jié)點(diǎn)下的某些屬性

3.增加,刪除,修改某些節(jié)點(diǎn)的文本

使用xml文檔

<?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>

實(shí)現(xiàn)思想
使用ElementTree,先將文件讀入,解析成樹(shù),之后,根據(jù)路徑,可以定位到樹(shù)的每個(gè)節(jié)點(diǎn),再對(duì)節(jié)點(diǎn)進(jìn)行修改,最后直接將其輸出

實(shí)現(xiàn)代碼

#!/usr/bin/python
# -*- coding=utf-8 -*-
# author : wklken@yeah.net
# date: 2012-05-25
# version: 0.1

from xml.etree.ElementTree import ElementTree,Element

def read_xml(in_path):
  '''讀取并解析xml文件
    in_path: xml路徑
    return: ElementTree'''
  tree = ElementTree()
  tree.parse(in_path)
  return tree

def write_xml(tree, out_path):
  '''將xml文件寫(xiě)出
    tree: xml樹(shù)
    out_path: 寫(xiě)出路徑'''
  tree.write(out_path, encoding="utf-8",xml_declaration=True)

def if_match(node, kv_map):
  '''判斷某個(gè)節(jié)點(diǎn)是否包含所有傳入?yún)?shù)屬性
    node: 節(jié)點(diǎn)
    kv_map: 屬性及屬性值組成的map'''
  for key in kv_map:
    if node.get(key) != kv_map.get(key):
      return False
  return True

#---------------search -----
def find_nodes(tree, path):
  '''查找某個(gè)路徑匹配的所有節(jié)點(diǎn)
    tree: xml樹(shù)
    path: 節(jié)點(diǎn)路徑'''
  return tree.findall(path)

def get_node_by_keyvalue(nodelist, kv_map):
  '''根據(jù)屬性及屬性值定位符合的節(jié)點(diǎn),返回節(jié)點(diǎn)
    nodelist: 節(jié)點(diǎn)列表
    kv_map: 匹配屬性及屬性值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):
  '''修改/增加 /刪除 節(jié)點(diǎn)的屬性及屬性值
    nodelist: 節(jié)點(diǎn)列表
    kv_map:屬性及屬性值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):
  '''改變/增加/刪除一個(gè)節(jié)點(diǎn)的文本
    nodelist:節(jié)點(diǎn)列表
    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):
  '''新造一個(gè)節(jié)點(diǎn)
    tag:節(jié)點(diǎn)標(biāo)簽
    property_map:屬性及屬性值map
    content: 節(jié)點(diǎn)閉合標(biāo)簽里的文本內(nèi)容
    return 新節(jié)點(diǎn)'''
  element = Element(tag, property_map)
  element.text = content
  return element

def add_child_node(nodelist, element):
  '''給一個(gè)節(jié)點(diǎn)添加子節(jié)點(diǎn)
    nodelist: 節(jié)點(diǎn)列表
    element: 子節(jié)點(diǎn)'''
  for node in nodelist:
    node.append(element)

def del_node_by_tagkeyvalue(nodelist, tag, kv_map):
  '''同過(guò)屬性及屬性值定位一個(gè)節(jié)點(diǎn),并刪除之
    nodelist: 父節(jié)點(diǎn)列表
    tag:子節(jié)點(diǎn)標(biāo)簽
    kv_map: 屬性及屬性值列表'''
  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. 讀取xml文件
  tree = read_xml("./test.xml")

  #2. 屬性修改
   #A. 找到父節(jié)點(diǎn)
  nodes = find_nodes(tree, "processers/processer")
   #B. 通過(guò)屬性準(zhǔn)確定位子節(jié)點(diǎn)
  result_nodes = get_node_by_keyvalue(nodes, {"name":"BProcesser"})
   #C. 修改節(jié)點(diǎn)屬性
  change_node_properties(result_nodes, {"age": "1"})
   #D. 刪除節(jié)點(diǎn)屬性
  change_node_properties(result_nodes, {"value":""}, True)

  #3. 節(jié)點(diǎn)修改
   #A.新建節(jié)點(diǎn)
  a = create_node("person", {"age":"15","money":"200000"}, "this is the firest content")
   #B.插入到父節(jié)點(diǎn)之下
  add_child_node(result_nodes, a)

  #4. 刪除節(jié)點(diǎn)
    #定位父節(jié)點(diǎn)
  del_parent_nodes = find_nodes(tree, "processers/services/service")
    #準(zhǔn)確定位子節(jié)點(diǎn)并刪除之
  target_del_node = del_node_by_tagkeyvalue(del_parent_nodes, "chain", {"sequency" : "chain1"})

  #5. 修改節(jié)點(diǎn)文本
    #定位節(jié)點(diǎn)
  text_nodes = get_node_by_keyvalue(find_nodes(tree, "processers/services/service/chain"), {"sequency":"chain3"})
  change_node_text(text_nodes, "new text")

  #6. 輸出到結(jié)果文件
  write_xml(tree, "./out.xml")

修改后的結(jié)果

<?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>

相關(guān)文章

  • Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開(kāi)及保存

    Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開(kāi)及保存

    本文將結(jié)合實(shí)例代碼,介紹Python3中PyQt5簡(jiǎn)單實(shí)現(xiàn)文件打開(kāi)及保存,具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-06-06
  • 深入理解python中的ThreadLocal

    深入理解python中的ThreadLocal

    本文主要介紹了深入理解python中的ThreadLocal,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 創(chuàng)建Shapefile文件并寫(xiě)入數(shù)據(jù)的例子

    創(chuàng)建Shapefile文件并寫(xiě)入數(shù)據(jù)的例子

    今天小編就為大家分享一篇?jiǎng)?chuàng)建Shapefile文件并寫(xiě)入數(shù)據(jù)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Python 使用 prettytable 庫(kù)打印表格美化輸出功能

    Python 使用 prettytable 庫(kù)打印表格美化輸出功能

    這篇文章主要介紹了Python 使用 prettytable 庫(kù)打印表格美化輸出功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • TensorFlow-gpu和opencv安裝詳細(xì)教程

    TensorFlow-gpu和opencv安裝詳細(xì)教程

    這篇文章主要介紹了TensorFlow-gpu和opencv安裝過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Sanic框架安裝與簡(jiǎn)單入門(mén)示例

    Sanic框架安裝與簡(jiǎn)單入門(mén)示例

    這篇文章主要介紹了Sanic框架安裝與簡(jiǎn)單用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Sanic框架的概念、原理、pip命令安裝以及使用方法,需要的朋友可以參考下
    2018-07-07
  • Python+selenium 獲取一組元素屬性值的實(shí)例

    Python+selenium 獲取一組元素屬性值的實(shí)例

    今天小編就為大家分享一篇Python+selenium 獲取一組元素屬性值的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • python 基礎(chǔ)學(xué)習(xí)第二彈 類(lèi)屬性和實(shí)例屬性

    python 基礎(chǔ)學(xué)習(xí)第二彈 類(lèi)屬性和實(shí)例屬性

    本人c程序員,最近開(kāi)始學(xué)python,深深的被python的強(qiáng)大所吸引,今后也會(huì)把學(xué)到的點(diǎn)點(diǎn)滴滴記錄下來(lái),現(xiàn)在分享一下關(guān)于類(lèi)屬性和實(shí)例屬性的一些問(wèn)題,很基礎(chǔ)的東西
    2012-08-08
  • Python?FastAPI?Sanic?Tornado?與Golang?Gin性能實(shí)戰(zhàn)對(duì)比

    Python?FastAPI?Sanic?Tornado?與Golang?Gin性能實(shí)戰(zhàn)對(duì)比

    本文將深入比較Python的FastAPI、Sanic、Tornado以及Golang的Gin框架的各種特性、性能表現(xiàn)以及適用場(chǎng)景,通過(guò)詳實(shí)的性能測(cè)試和實(shí)際示例代碼,將探討它們?cè)跇?gòu)建現(xiàn)代高性能應(yīng)用中的優(yōu)劣勢(shì),以便開(kāi)發(fā)者根據(jù)需求做出明智的選擇
    2024-01-01
  • python多線程共享變量的使用和效率方法

    python多線程共享變量的使用和效率方法

    今天小編就為大家分享一篇python多線程共享變量的使用和效率方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07

最新評(píng)論