python添加xml节点
㈠ python写xml中文本节点怎么才能不换行
1、在python 2.x中,在print 'xxxx' 后面加一个逗号
print 'hello world',
2、python 3.x中,需要在函数内增加一个end字段:
print('hello world', end='')
㈡ 如何编写一个程序从xml文件里面提取一些关键的节点数据组成新的xml文件,最好是用python编写
给你一段代码,供参考:
fromsgmllibimportSGMLParser
importhtmlentitydefs
classBaseHTMLProcessor(SGMLParser):
defreset(self):
#extend(calledbySGMLParser.__init__)
self.pieces=[]
SGMLParser.reset(self)
defunknown_starttag(self,tag,attrs):
#calledforeachstarttag
#attrsisalistof(attr,value)tuples
#e.g.for<preclass="screen">,tag="pre",attrs=[("class","screen")]
#,but
#'tquotedinthesource
#document,
#(singletodoublequotes).
#Notethatimproperlyembeddednon-HTMLcode(likeclient-sideJavascript)
#,causingruntimescripterrors.
#Allnon-(<!--code-->)
#(inhandle_comment).
strattrs="".join(['%s="%s"'%(key,value)forkey,valueinattrs])
self.pieces.append("<%(tag)s%(strattrs)s>"%locals())
defunknown_endtag(self,tag):
#calledforeachendtag,e.g.for</pre>,tagwillbe"pre"
#Reconstructtheoriginalendtag.
self.pieces.append("</%(tag)s>"%locals())
defhandle_charref(self,ref):
#,e.g.for" ",refwillbe"160"
#.
self.pieces.append("&#%(ref)s;"%locals())
defhandle_entityref(self,ref):
#calledforeachentityreference,e.g.for"&;",refwillbe""
#.
self.pieces.append("&%(ref)s"%locals())
#;otherentitiesarenot
ifhtmlentitydefs.entitydefs.has_key(ref):
self.pieces.append(";")
defhandle_data(self,text):
#calledforeachblockofplaintext,i.e.outsideofanytagand
#
#Storetheoriginaltextverbatim.
self.pieces.append(text)
defhandle_comment(self,text):
#calledforeachHTMLcomment,e.g.<!--insertJavascriptcodehere-->
#Reconstructtheoriginalcomment.
#-side
#code(likeJavascript)
#processorundisturbed;seecommentsinunknown_starttagfordetails.
self.pieces.append("<!--%(text)s-->"%locals())
defhandle_pi(self,text):
#,e.g.<?instruction>
#.
self.pieces.append("<?%(text)s>"%locals())
defhandle_decl(self,text):
#calledfortheDOCTYPE,ifpresent,e.g.
#<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"
# "http://www.w3.org/TR/html4/loose.dtd">
#ReconstructoriginalDOCTYPE
self.pieces.append("<!%(text)s>"%locals())
defoutput(self):
""""""
return"".join(self.pieces)
if__name__=="__main__":
fork,vinglobals().items():
printk,"=",v
㈢ 请问用python怎么修改xml的节点值
from xml.etree import ElementTree
filePath = 'd:\\aaa.xml'
xmldoc = ElementTree.parse(filePath)
node = xmldoc.find('./string')
node.text = "win 7"
其实说实话,楼上的回答已经很清楚了.
不管什么编程语言,要想学好,基础一定要扎实.
㈣ python怎么处理xml节点包含命名空间,也就是冒号的情况
a:b为名不行吧,要展开为{URI}b这种形式,看看下面小例子取出的tag名称:
# -*- coding: utf-8 -*-
from xml.etree import ElementTree as ET
import cStringIO
xml = """\
<?xml version="1.0"?>
<root xmlns = "http://default-namespace.org/"
xmlns:py = "http://www.python.org/ns/">
<py:elem1 />
<elem2 xmlns="" />
</root>
"""
f = cStringIO.StringIO(xml)
#find all elements and print tag's name.
tree = ET.parse(f)
print repr(tree.getroot().tag)
elems = tree.findall('.//*')
for elem in elems:
print repr(elem.tag)
#same as above, but using iterparse.
f.seek(0)
for event, elem in ET.iterparse(f, ("start",)):
print repr(elem.tag)
输出:
'{http://default-namespace.org/}root'
'{http://www.python.org/ns/}elem1'
'elem2'
'{http://default-namespace.org/}root'
'{http://www.python.org/ns/}elem1'
'elem2'
㈤ 怎么用python获取xml文件的所有节点。
假如我们有个xml文档如下:example.xml
<?xml version="1.0" encoding="UTF-8"?>
<BIT>
<GeneralDescription>
<name>Matlab</name>
<DateTime>2008-09-10 23:16:48</DateTime>
</GeneralDescription>
<AMatrix>1,2,3;4,5,6;7,8,9</AMatrix>
</BIT>
首先,要解析xml文档,需要导入一个模块
>>> from xml.dom.minidom import parse
(1)然后载入一个xml文档
>>> xmldoc = parse("J:/homeword/example.xml")
>>> print xmldoc.toxml()
<?xml version="1.0" ?>
<BIT>
<GeneralDescription>
<name>Matlab</name>
<DateTime>2008-09-10 23:16:48</DateTime>
</GeneralDescription>
<AMatrix>1,2,3;4,5,6;7,8,9</AMatrix>
</BIT>
>>>
上面是可以查看这个文档的内容。
Toxml方法打印出了node风格的xml,如果节点是Document结果,则打印出整个xml文档。否则只打印出本节点所属内容。
(2)如何获取子节点
>>> xmldoc.childNodes
[<DOM Element: BIT at 0x1223af8>]
>>>
每一个node都有一个childNodes的属性,他是一个node对象的列表,注意的是,一个Document只有一个子节点,上例中就是BIT这个节点,它属于Document节点。
因为是列表,所以也可以同用列表索引,xmldoc.childNodes[0]
>>> BIT_element = xmldoc.firstChild
>>> BIT_element
<DOM Element: BIT at 0x1223af8>
>>> print BIT_element.toxml()
<BIT>
<GeneralDescription>
<name>Matlab</name>
<DateTime>2008-09-10 23:16:48</DateTime>
</GeneralDescription>
<AMatrix>1,2,3;4,5,6;7,8,9</AMatrix>
</BIT>
>>>
(3)获得某一个节点的文本
>>> BIT_element.childNodes
[<DOM Text node "
">, <DOM Element: GeneralDescription at 0x1223be8>, <DOM Text node "
">, <DOM Element: AMatrix at 0x1223e40>, <DOM Text node "
">]
>>>name = (BIT_element.childNodes[1]).childNodes[1]
>>> >>> name.childNodes[0].data
u'Matlab'
>>>
㈥ python操作xml文件问题
我给你个示例代码,你自己改改增加子节点那一段就好了。
#!/usr/bin/python
# -*- coding=utf-8 -*-
# author : [email protected]
# date: 2012-05-25
# version: 0.1
from xml.etree.ElementTree import ElementTree,Element
def read_xml(in_path):
'''读取并解析xml文件
in_path: xml路径
return: ElementTree'''
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree, out_path):
'''将xml文件写出
tree: xml树
out_path: 写出路径'''
tree.write(out_path, encoding="utf-8",xml_declaration=True)
def if_match(node, kv_map):
'''判断某个节点是否包含所有传入参数属性
node: 节点
kv_map: 属性及属性值组成的map'''
for key in kv_map:
if node.get(key) != kv_map.get(key):
return False
return True
#---------------search -----
def find_nodes(tree, path):
'''查找某个路径匹配的所有节点
tree: xml树
path: 节点路径'''
return tree.findall(path)
def get_node_by_keyvalue(nodelist, kv_map):
'''根据属性及属性值定位符合的节点,返回节点
nodelist: 节点列表
kv_map: 匹配属性及属性值map'''
result_nodes = []
for node in nodelist:
if if_match(node, kv_map):
result_nodes.append(node)
return result_nodes
#---------------change -----
def change_node_properties(nodelist, kv_map, is_delete=False):
'''修改/增加 /删除 节点的属性及属性值
nodelist: 节点列表
kv_map:属性及属性值map'''
for node in nodelist:
for key in kv_map:
if is_delete:
if key in node.attrib:
del node.attrib[key]
else:
node.set(key, kv_map.get(key))
def change_node_text(nodelist, text, is_add=False, is_delete=False):
'''改变/增加/删除一个节点的文本
nodelist:节点列表
text : 更新后的文本'''
for node in nodelist:
if is_add:
node.text += text
elif is_delete:
node.text = ""
else:
node.text = text
def create_node(tag, property_map, content):
'''新造一个节点
tag:节点标签
property_map:属性及属性值map
content: 节点闭合标签里的文本内容
return 新节点'''
element = Element(tag, property_map)
element.text = content
return element
def add_child_node(nodelist, element):
'''给一个节点添加子节点
nodelist: 节点列表
element: 子节点'''
for node in nodelist:
node.append(element)
def del_node_by_tagkeyvalue(nodelist, tag, kv_map):
'''同过属性及属性值定位一个节点,并删除之
nodelist: 父节点列表
tag:子节点标签
kv_map: 属性及属性值列表'''
for parent_node in nodelist:
children = parent_node.getchildren()
for child in children:
if child.tag == tag and if_match(child, kv_map):
parent_node.remove(child)
if __name__ == "__main__":
#1. 读取xml文件
tree = read_xml("./test.xml")
#2. 属性修改
#A. 找到父节点
nodes = find_nodes(tree, "processers/processer")
#B. 通过属性准确定位子节点
result_nodes = get_node_by_keyvalue(nodes, {"name":"BProcesser"})
#C. 修改节点属性
change_node_properties(result_nodes, {"age": "1"})
#D. 删除节点属性
change_node_properties(result_nodes, {"value":""}, True)
#3. 节点修改
#A.新建节点
a = create_node("person", {"age":"15","money":"200000"}, "this is the firest content")
#B.插入到父节点之下
add_child_node(result_nodes, a)
#4. 删除节点
#定位父节点
del_parent_nodes = find_nodes(tree, "processers/services/service")
#准确定位子节点并删除之
target_del_node = del_node_by_tagkeyvalue(del_parent_nodes, "chain", {"sequency" : "chain1"})
#5. 修改节点文本
#定位节点
text_nodes = get_node_by_keyvalue(find_nodes(tree, "processers/services/service/chain"), {"sequency":"chain3"})
change_node_text(text_nodes, "new text")
#6. 输出到结果文件
write_xml(tree, "./out.xml")
㈦ python怎么向已经存在的xml文件中追加填入数据
直接操作文件肯定不现实,只能先使用python自带模块xml.etree 解析xml,然后找到对应的节点把数据添加进去,重新写入文件。
㈧ 如何使用Python和xml.etree.ElementTree解析xml文件获取其节点
<?xmlversion="1.0"encoding="utf-8"?>
<root>
<bodyname="lyc">
<age>110</age>
</body>
<bodyname="l"age="10">
</body>
</root>
######################
#coding=UTF8
fromxml.etreeimportElementTree
#xmlText=open("xml.txt").read()
#root=ElementTree.fromstring(xmlText)
root=ElementTree.parse("xml.txt")
bodys=root.getiterator("body")
#getiterator方法获取
print"getiterator"
printbodys
printdir(bodys[0])
print"attrib:",bodys[0].attrib
print"tag:",bodys[0].tag
print"text",bodys[0].text
#getchildren方法获取
print"getchildren"
children=bodys[0].getchildren()
printchildren
print"attrib:",children[0].attrib
print"tag:",children[0].tag
print"text:",children[0].text
#find
print"find"
children=root.find("body")
printchildren
print"attrib:",children.attrib
print"tag:",children.tag
print"text:",children.text
#findall
print"findall"
children=root.findall("body")
printchildren
print"attrib:",children[0].attrib
print"tag:",children[0].tag
print"text:",children[0].text