#coding=utf-8
from xml.dom import minidom
from xml.dom.minidom import Document
import xml
def writeXML(filaName="test.xml"):
doc = Document()
feature=doc.createElement("feature")
doc.appendChild(feature)
father=doc.createElement("father")
father.setAttribute('name','noun') #元素属性
text = doc.createTextNode('系统')#元素值 feature.appendChild(father)
father.appendChild(text)
son=doc.createElement("son")
text = doc.createTextNode('系统')#元素值
son.appendChild(text)
father.appendChild(son)
f = open(filaName,'w')
f.write(doc.toprettyxml(indent = ''))
f.close()
def readXML(fileName="test.xml"):
dom = xml.dom.minidom.parse(fileName) #打开xml文档
root = dom.documentElement #得到文档元素对象
bb = root.getElementsByTagName('father')
b=bb[0]
print b.nodeName
print b.nodeValue
print b.nodeType
print b.getAttribute("name")
print b.firstChild.data.encode("utf-8")
def main():
writeXML()
# readXML()
if __name__=="__main__":
main()

写入的xml文档内容:

<?xml version="1.0" ?>
<feature>
<father name="noun">
系统
<son>系统</son>
</father>
</feature>

可能写入的xml文档格式不是很好看,显示父子关系不好,可通过文本写入的方式,调整xml的格式。

对于xml的每个节点有三种属性:

nodeName为结点名字。

nodeValue是结点的值,只对文本结点有效。

nodeType是结点的类型。catalog是ELEMENT_NODE类型

第一个系统是father的标签之间的数据。

 #coding=utf-8
from xml.dom import minidom
from xml.dom.minidom import Document
import xml
def writeXML(filaName="test.xml"):
doc = Document()
feature=doc.createElement("feature")
doc.appendChild(feature)
father=doc.createElement("father")
father.setAttribute('name','noun') #元素属性
text = doc.createTextNode('系统')#元素值 feature.appendChild(father)
father.appendChild(text)
son=doc.createElement("son")
text = doc.createTextNode('系统')#元素值
son.appendChild(text)
father.appendChild(son)
f = open(filaName,'w')
f.write(doc.toprettyxml(indent = ''))
f.close()
def readXML(fileName="test.xml"):
dom = xml.dom.minidom.parse(fileName) #打开xml文档
root = dom.documentElement #得到文档元素对象
bb = root.getElementsByTagName('father')
b=bb[0]
print b.nodeName
print b.nodeValue
print b.nodeType
print b.getAttribute("name")
print b.firstChild.data.encode("utf-8")
def main():
# writeXML()
readXML()
if __name__=="__main__":
main()
'''
输出:
father
None
1
noun
系统
[Finished in 0.1s]
'''

Python文件之----XML的更多相关文章

  1. python专题-读取xml文件

    关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...

  2. 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件

    XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...

  3. python读取/创建XML文件

    Python中定义了很多处理XML的函数,如xml.dom,它会在处理文件之前,将根据xml文件构建的树状数据存在内存.还有xml.sax,它实现了SAX API,这个模块牺牲了便捷性,换取了速度和减 ...

  4. 【304】python专题-读取xml文件

    参考:XML DOM 参考手册(w3school) 参考:python专题-读取xml文件 参考:请问用python怎么修改xml的节点值? 1. 读取标签内的文本(Python) 如下的 xml 文 ...

  5. Python:Dom生成XML文件(写XML)

    http://www.ourunix.org/post/327.html 在python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文 ...

  6. python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用

    1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...

  7. python批量json文件转xml文件脚本(附代码)

    场景:在使用了mask rcnn跑实验后标注了大量地json格式文件,现在打算使用yolo和faster rcnn 跑实验 所以需要将之前地json文件转为xml     但是找了很久,没发现有批量处 ...

  8. web端自动化——Python读取txt文件、csv文件、xml文件

    1.读取txt文件 txt文件是我们经常操作的文件类型,Python提供了以下几种读取txt文件的方式. 1)read(): 读取整个文件. 2)readline(): 读取一行数据. 3)readl ...

  9. 用 ElementTree 在 Python 中解析 XML

    用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...

随机推荐

  1. AT24C512与AT24C512B的区别

    前几日公司采购人员说现在AT24C512要停产了,替代型号为AT24C512B,因为公司产品中使用该器件较多,因此专门又研究了一下这两个芯片,发现还是有很多地方不同的. (1)AT24C512输入电压 ...

  2. 自己动手实现智能指针auto_ptr

    面试的时候,我们经常会被问到如何自己动手实现智能指针auto_ptr.今天我就一边参考STL库中的源代码,一边将auto_ptr的实现敲一遍. auto_ptr归根到底是一个模版类,那么这个类要实现哪 ...

  3. 【转】基于 Android NDK 的学习之旅-----数据传输(引用数据类型)

    原文网址:http://www.cnblogs.com/luxiaofeng54/archive/2011/08/20/2147086.html 基于 Android NDK 的学习之旅-----数据 ...

  4. CGI编程完全手册

    一.基本原理 CGI:通用网关接口(Common Gateway Interface)是一个Web服务器主机提供信息服务的标准接口.通过CGI接口,Web服务器就能够获取客户端提交的信息,转交给服务器 ...

  5. 开源的asp.net工作流程引擎。 http://ccflow.org

    开源的asp.net工作流程引擎. http://ccflow.org

  6. C数组的相关知识

    数组的定义:具有相同数据类型的集合.在内存中开辟连续的存贮空间,从上往下,依次存储 补充:内存是以字节位单位的存储空间,内存中的每一个字节都唯一对应一个编号.这个编号就是地址.只要是存在内存中的数据都 ...

  7. [Locked] Alien Dictionary

    Alien Dictionary There is a new alien language which uses the latin alphabet. However, the order amo ...

  8. Apache-Tika解析JPEG文档

    通常在使用爬虫时,爬取到网上的文章都是各式各样的格式处理起来比较麻烦,这里我们使用Apache-Tika来处理JPEG格式的图片,如下: package com.mengyao.tika.app; i ...

  9. the identity used to sign the executable is no longer valid.解决方法

    the identity used to sign the executable is no longer valid.解决方法 一.重新下载Provisioning Profile 1.到devel ...

  10. awr报告

    BEGIN DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT();END;/ exec DBMS_WORKLOAD_REPOSITORY.CREATE_SNAPSHOT ...