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