首页 > 编程语言 >python处理xml文件

python处理xml文件

时间:2023-02-16 18:45:25浏览次数:36  
标签:xml 文件 dom python country print root 节点

# pip install pyyaml  环境里先安装包


import xml.dom.minidom dom = xml.dom.minidom.parse('config.xml') root = dom.documentElement def xml(suser): suser = root.getElementsByTagName(suser) return suser[0].firstChild.data id = xml('id') # 进程名 print("打印ID:"+id) import xml.etree.ElementTree as ET # 从文件加载并解析 XML 数据 tree = ET.parse('country_data.xml') root = tree.getroot() print(root.tag) # 打印根节点名称 print(root.attrib) # 打印根节点属性 # for 循环可以列出所有的子节点: # 子节点与属性 for child in root: print(child.tag, child.attrib) #使用索引的方式存取任意的节点 print(root[0][1].text) #get 直接取得指定的属性值 print(root[0][3].get('name')) #iter 可以在指定节点之下,以递回方式搜索所有子节点: for neighbor in root.iter('neighbor'): print(neighbor.attrib) # find 则是只从第一层子节点中搜索(不包含第二层以下),findall 会传回所有结果,而 find 则是只传回第一个找到的节点: # 只从第一层子节点中搜索,传回所有找到的节点 for country in root.findall('country'): # 只从第一层子节点中搜索,传回第一个找到的节点 rank = country.find('rank').text # 取得节点指定属性质 name = country.get('name')

附录:

config.xml

<config>
    <id>905594711349653</id>
    <sec>0tn1jeerioj4x6lcugdd8xmzvm6w42tp</sec>
</config>

 

country_data.xml

<?xml version="1.0"?>
<data>
    <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>

 

标签:xml,文件,dom,python,country,print,root,节点
From: https://www.cnblogs.com/yclh/p/17127903.html

相关文章

  • Python sorted函数及用法
    orted()作为 Python 内置函数之一,其功能是对序列(列表、元组、字典、集合、还包括字符串)进行排序。sorted()函数的基本语法格式如下:list=sorted(iterable,key=None,......
  • 实践:基于腾讯云播放器SDK,带您体验播放多场景下的 COS 视频文件
    一.实践步骤1.准备您的腾讯云COS视频文件链接,您需要:1.1创建一个存储桶;1.2上传对象;1.3在对象信息详情里复制对象地址;注意:    目前腾讯云......
  • python set集合
    使用花括号{和}来创建一个包含多个元素的集合。集合和字典之间的区别在于,字典要求我们键入由冒号:分隔的键值对,而集合则不需要。  4.集合中添加新的元素我们一般使用......
  • shell 判断文件夹或文件是否存在
    文件夹不存在则创建if[!-d"/data/"];thenmkdir/dataelseecho"文件夹已经存在"fi文件存在则删除if[!-f"/data/filename"];thenecho"文件不存在"els......
  • python对文件的处理方法
    #1、打开文件如果文件不存在会报错file=open("1.txt")#2、使用w、w+、a、a+模式打开,如果文件不存在就创建文件file=open("1.txt","w")#3、指定绝对路径路径f......
  • 【android】音视频开发五:学习MediaExtractor 和 MediaMuxer,知道如何解析和封装 mp4 文
    MediaExtractorMediaExtractor顾名思义就是多媒体提取器,主要负责:获取媒体文件的格式,包括音视频轨道,编码格式,宽高,采样率,声道数等,分离音频流,视频流,读取分离后的音视频数据......
  • python logging日志没有写入到指定文件,写到其他项目的日志文件
    背景:项目A为主框架项目,使用到了项目B的方法项目A、B均有封装好的日志方法,且均在封装好的日志文件里面,增加了logger=MyLogger().info,其他文件要使用日志时,引入logger进......
  • 前端通过post下载文件,文件乱码的解决
    有时候数据量大或者需要上传文件,但接口又必须返回一个下载的流,就必须前端设置一下进行下载过程也很简单网上一搜一大堆博客,标红的地方必须这样写其余的可以根据你的需求......
  • WebUploader上传大文件的三种解决方案
    ​ 第一点:Java代码实现文件上传FormFilefile=manform.getFile();StringnewfileName= null;Stringnewpathname= null;StringfileAddre= "/numUp";try{......
  • Pandoc 将 markdown 文件转化为 pdf 命令
    之前查阅其它博客发现命令会报错,经尝试,现在使用Pandoc将markdown文件转化为pdf文件的命令如下:pandoc-s--toc--pdf-engine=xelatex-VCJKmainfont="SimSun"-V......