Python文件之----XML
#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的更多相关文章
- python专题-读取xml文件
关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...
- 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件
XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...
- python读取/创建XML文件
Python中定义了很多处理XML的函数,如xml.dom,它会在处理文件之前,将根据xml文件构建的树状数据存在内存.还有xml.sax,它实现了SAX API,这个模块牺牲了便捷性,换取了速度和减 ...
- 【304】python专题-读取xml文件
参考:XML DOM 参考手册(w3school) 参考:python专题-读取xml文件 参考:请问用python怎么修改xml的节点值? 1. 读取标签内的文本(Python) 如下的 xml 文 ...
- Python:Dom生成XML文件(写XML)
http://www.ourunix.org/post/327.html 在python中解析XML文件也有Dom和Sax两种方式,这里先介绍如何是使用Dom解析XML,这一篇文章是Dom生成XML文 ...
- python解析xml文件之xml.etree.cElementTree和xml.etree.ElementTree区别和基本使用
1.解析速度:ElementTree在 Python 标准库中有两种实现.一种是纯 Python 实现例如 xml.etree.ElementTree ,另外一种是速度快一点的 xml.etree.c ...
- python批量json文件转xml文件脚本(附代码)
场景:在使用了mask rcnn跑实验后标注了大量地json格式文件,现在打算使用yolo和faster rcnn 跑实验 所以需要将之前地json文件转为xml 但是找了很久,没发现有批量处 ...
- web端自动化——Python读取txt文件、csv文件、xml文件
1.读取txt文件 txt文件是我们经常操作的文件类型,Python提供了以下几种读取txt文件的方式. 1)read(): 读取整个文件. 2)readline(): 读取一行数据. 3)readl ...
- 用 ElementTree 在 Python 中解析 XML
用 ElementTree 在 Python 中解析 XML 原文: http://eli.thegreenplace.net/2012/03/15/processing-xml-in-python- ...
随机推荐
- Struts2 权限验证
之前的Struts2项目通过再Sitemesh的母版页中使用Struts的if标签进行了session判断,使得未登录的用户不能看到页面,但是这 种现仅仅在view层进行,如果未登录用户直接在地址栏输 ...
- 【HDOJ】2896 病毒侵袭
AC自动机模板题. #include <iostream> #include <cstdio> #include <cstring> #include <qu ...
- Apache HTTP Server mod_session_dbd模块mod_session_dbd.c 安全漏洞
漏洞名称: Apache HTTP Server mod_session_dbd模块mod_session_dbd.c 安全漏洞 CNNVD编号: CNNVD-201307-488 发布时间: 201 ...
- 使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
原文地址:http://blog.csdn.net/hawksoft/article/details/21776009 最近调试原来的微信模拟登陆时发生了“基础连接已关闭,发送时发生错误”的错误提示, ...
- bzoj1227 [SDOI2009]虔诚的墓主人(组合公式+离散化+线段树)
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MBSubmit: 803 Solved: 372[Submit][Statu ...
- Servlet线程安全
public class servletDemo1 extends HttpServlet { int i=0; public void doGet(HttpServletRequest reques ...
- Flex3在应用RemoteObject出现问题解决方法
出现该问题 <mx:RemoteObject id="robj" destination="hello" endpoint="http://lo ...
- Solr系列二:Solr与mmseg4j的整合
mmseg4j是一个很好的中文分词器,solr与mmseg4j的整合也非常简单.如下: 第一步:下载mmseg4j的jar包,网上搜索一下有很多下载地址,如下是csdn上的一个连接:http://do ...
- jiaocheng https://github.com/CarpenterLee/JCFInternals
https://github.com/CarpenterLee/JCFInternals
- linux防火墙 基础知识
转 http://drops.wooyun.org/tips/1424 iptables介绍 linux的包过滤功能,即linux防火墙,它由netfilter 和 iptables 两个组件组成. ...