zoukankan      html  css  js  c++  java
  • 实例Python处理XML文件的方法

    转自:http://www.jb51.net/article/71773.htm

    以下是部分代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 表名 ,数据库名  可灵活配置插入哪个库哪个表 -->
    <table name="top_query" db_name="evaluting_sys">
    <!-- 非业务主键,自增长,可配名,其他 INTEGER UNSIGNED AUTO_INCREMENT -->
    <primary_key>
    <name>id</name>
    </primary_key>
    <!-- 字段开始 -->
    <field>
    <name>query</name>
    <type>varchar(200)</type>
    <is_index>false</is_index>
    <description>query</description>
    </field>
    <field>
    <name>pv</name>
    <type>integer</type>
    <is_index>false</is_index>
    <description>pv</description>
    </field>
    <field>
    <name>avg_money</name>
    <type>integer</type>
    <is_index>false</is_index>
    <description></description>
    </field>
    <!-- 字段配置结束 -->
    </table>

    ----------------------------------

    import sys,os
    from xml.dom import minidom, Node
     
    def read_dbconfig_xml(xml_file_path):
      content = {}
     
      root = minidom.parse(xml_file_path)
      table = root.getElementsByTagName("table")[0]
     
      #read dbname and table name.
      table_name = table.getAttribute("name")
      db_name = table.getAttribute("db_name")
     
      if len(table_name) > 0 and len(db_name) > 0:
        db_sql = "create database if not exists `" + db_name +"`; use " + db_name + ";"
        table_drop_sql = "drop " + table_name + " if exists " + table_name + ";"
        content.update({"db_sql" : db_sql})
        content.update({"table_sql" : table_drop_sql })
      else:
        print "Error:attribute is not define well! db_name=" + db_name + " ;table_name=" + table_name
        sys.exit(1)
      #print table_name, db_name
     
      table_create_sql = "create table " + table_name +"("
     
      #read primary cell
      primary_key = table.getElementsByTagName("primary_key")[0]
      primary_key_name = primary_key.getElementsByTagName("name")[0].childNodes[0].nodeValue
     
      table_create_sql += primary_key_name + " INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY,"
     
      #print primary_key.toxml()
      #read ordernary field
      fields = table.getElementsByTagName("field")
      f_index = 0
      for field in fields:
        f_index += 1
        name = field.getElementsByTagName("name")[0].childNodes[0].nodeValue
        type = field.getElementsByTagName("type")[0].childNodes[0].nodeValue
        table_create_sql += name + " " + type
        if f_index != len(fields):
        table_create_sql += ","
        is_index = field.getElementsByTagName("is_index")[0].childNodes[0].nodeValue
     
      table_create_sql += ");"
      content.update({"table_create_sql" : table_create_sql})
      #character set latin1 collate latin1_danish_ci;
      print content
     
     
    if __name__ == "__main__":
    read_dbconfig_xml(sys.argv[1])
  • 相关阅读:
    JAVA005-基本数据类型变量的存储
    JAVA003-变量、数据类型
    Python_pandas数据处理_学习
    python_性能FPS
    DB_004_创建表
    DB_003_关系数据库标准语言(SQL)
    DB_002_数据库的创建和管理
    DB_001_概念模型设计
    虚幻蓝图学习笔记 简单VR功能实现
    虚幻蓝图学习笔记 制作第一人称(实现功能:捡枪,换枪,扔枪,仍炸弹等)(一)
  • 原文地址:https://www.cnblogs.com/fengff/p/8467389.html
Copyright © 2011-2022 走看看