读写xml文件

以下面dameon来介绍读取xml文件

from xml.dom import minidom
#创建DOM对象
dom=minidom.Document()
#创建根节点。每次都要用DOM对象来创建任何节点
root_node = dom.createElement('info')
#用DOM对象添加根元素
dom.appendChild(root_node)
base_node = dom.createElement('base')
root_node.appendChild(base_node)
platform_node = dom.createElement('platfrom')
base_node.appendChild(platform_node)
platform_text=dom.createTextNode('Windows')
platform_node.appendChild(platform_text)
br_node = dom.createElement('browser')
base_node.appendChild(br_node)
br_text=dom.createTextNode('Firefox')
br_node.appendChild(br_text)
url_node=dom.createElement('url')
base_node.appendChild(url_node)
url_text=dom.createTextNode('http://www.baidu.com')
url_node.appendChild(url_text)
login_node_01=dom.createElement('login')
base_node.appendChild(login_node_01)
login_node_01.setAttribute('username','admin')
login_node_01.setAttribute('password','123456')
login_node_02=dom.createElement('login')
base_node.appendChild(login_node_02)
#设置节点的属性
login_node_02.setAttribute('username','guest')
login_node_02.setAttribute('password','654321')
test_node = dom.createElement('test')
root_node.appendChild(test_node)
pro_01=dom.createElement('province')
test_node.appendChild(pro_01)
#用DOM创建文本节点,把文本节点(文字内容)看成子节点
pro_text_01=dom.createTextNode('北京')
pro_01.appendChild(pro_text_01)
pro_02=dom.createElement('province')
test_node.appendChild(pro_02)
pro_text_02=dom.createTextNode('广东')
pro_02.appendChild(pro_text_02)
city_01=dom.createElement('city')
test_node.appendChild(city_01)
city_text_01=dom.createTextNode('深圳')
city_01.appendChild(city_text_01)
city_02=dom.createElement('city')
test_node.appendChild(city_02)
city_text_02=dom.createTextNode('珠海')
city_02.appendChild(city_text_02)
pro_03 = dom.createElement('province')
test_node.appendChild(pro_03)
pro_text_03=dom.createTextNode('浙江')
pro_03.appendChild(pro_text_03)
city_03=dom.createElement('city')
test_node.appendChild(city_03)
city_text_03=dom.createTextNode('杭州')
city_03.appendChild(city_text_03)

try:
    with open('info.xml','w',encoding='utf-8') as fp:
        dom.writexml(fp,indent='',addindent='\t',newl='\r',encoding='utf-8')
        print('write ok')
except Exception as e:
    print(e)

读取xml 文件

from xml.dom import minidom
#打开xml文档
dom=minidom.parse('info.xml')
#得到文档元素对象
root=dom.documentElement
print(root.nodeName)
print(root.nodeValue)
print(root.nodeType)
print(root.ELEMENT_NODE)
tagname = root.getElementsByTagName('browser')
print(tagname[0].tagName)
tagname=root.getElementsByTagName('login')
print(tagname[0].tagName)
username=tagname[0].getAttribute('username')
print(username)
#获取元素的属性
password=tagname[0].getAttribute('password')
print(password)
provinces=dom.getElementsByTagName('province')
#获取标签对的值
p1=provinces[1].firstChild.data
print(p1)
citys=dom.getElementsByTagName('city')
c1=citys[0].firstChild.data
print(c1)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# XML文件读写操作源码,以及如何调用,注释详解,有任何问题请留言, 以下截取xml文件和部分调用代码段: * ++++++++++++++++++++++++++++++++++++++ <?xml version="1.0" encoding="utf-8" standalone="no"?> <!--TestPlugins的信息--> <!--DataPlugins的信息--> * ++++++++++++++++++++++++++++ xml xl = new xml(); xl.XMLWriteRootNode("info"); //XmlElement Eml1 = xl.XMLReadNode("",0); //XmlElement Eml2 = xl.XMLReadNode("DataPlugins", 1); //XmlElement Eml4 = xl.XMLReadNode("DeviceInfo", 2); // TestPlugins XmlElement testPlugins = xl.XMLCreateNode("TestPlugins", null, null); xl.XMLInsertNode("info", 0, "TestPlugins的信息", testPlugins); // FixturePlugin XmlElement fixturePlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "FixturePlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, fixturePlugin); // DUTPlugin XmlElement DUTPlugin = (XmlElement)fixturePlugin.CloneNode(true);// xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "DUTPlugin" } }); DUTPlugin.SetAttribute("Type", "DUTPlugin"); xl.XMLInsertNode(testPlugins.LocalName, 1, null, DUTPlugin); // Agilent34461APlugin XmlElement Agilent34461APlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "Agilent34461APlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, Agilent34461APlugin); // ================================== // DataPlugins XmlElement dataPlugins = xl.XMLCreateNode("DataPlugins", null, null); xl.XMLInsertNode("info", 0, "DataPlugins的信息", dataPlugins); // CSVLogPlugin XmlElement csvlogPlugin = xl.XMLCreateNode("DataPlugin", null, new Dictionary() { { "Type", "CSVLogPlugin" } }); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, csvlogPlugin); XmlElement uartlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); uartlogPlugin.SetAttribute("Type", "UartLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, uartlogPlugin); XmlElement testlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); testlogPlugin.SetAttribute("Type", "TestLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, testlogPlugin); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值