pythonelementtree
㈠ python elementtree 生成包含<![CDATA[ ]]>的xml文件
http://stackoverflow.com/questions/174890/how-to-output-cdata-using-elementtree
㈡ 在python中導入from nltk.etree.ElementTree import ElementTree這個模塊時為什麼老出錯
網上隨便一搜,就有解決辦法了:
1.確保自己的文件,不要叫做xml.py。換個其他任意名字均可。
2.確保自己最開始的位置,加上這句:
import xml.etree.ElementTreeas 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
㈣ python解析xml:xml.etree.ElementTree似乎存在bug
<?xml version = "1.0" encoding = "utf-8"?>
<root>
<body name="lyc">
<age>110</age>
</body>
<body name = "l" age = "10">
</body>
</root>
㈤ python 中 ElementTree按以下方法是用對嗎dom = ElementTree(file=urllib.urlopen(SPLDetail_command))
不對啊,
file=urllib.urlopen(SPLDetail_command).read()
dom = ElementTree.parse(file)
㈥ python elementtree 判斷節點是否有子節點
lxml takes all the pain out of XML.
Stephan Richter
lxml是Python語言里和XML以及html工作的功能最豐富和最容易使用的庫。lxml是為libxml2和libxslt庫的一個Python化的綁定。它與眾不同的地方是它兼顧了這些庫的速度和功能完整性,以及純Python API的簡潔性,大部分與熟知的ElementTree API兼容但比之更優越。
安裝lxml:
要求:需要Python2.3或更後的版本
使用easy_install工具,以超級用戶或管理員的角色run下面的命令:
easy_install lxml
在windows下,最好指定版本號:easy_install lxml==2.2.6
使用lxml進行開發
lxml.etree指南
通常使用lxml.etree的方式
>>> from lxml import etree
Element類,一個Element是ElementTree API的主要容器類,大部分的XML tree功能都是通過這個類來訪問的。Elements可以非常容易地通過Element工廠方法來創建。
>>> root = etree.Element("root")
元素的XML tag名字是通過tag屬性來訪問的
>>> print root.tag # root
Elements是在XML樹狀結構中組織的,為創建子元素並將它們加到父元素上,可以使用append()方法。
>>> root.append( etree.Element("child1") )
我們還有更高效的方法:SubElement工廠方法,它使用和Element工廠方法相同的參數,不過額外需要父節點作第一個參數:
>>> child2 = etree.SubElement(root, "child2")
>>> child3 = etree.SubElement(root, "child3")
可以使用tostring()方法來看得到的XML
>>> print etree.tostring(root, pretty_print=True)
<root>
<child1/>
<child2/>
<child3/>
</root>
元素是列表
>>> child = root[0]
>>> print child.tag
child1
>>> print len(root)
3
>>> root.index(root[1]) # lxml.etree only!
1
列印所有子節點:
>>> children = list(root)
>>> for child in root:
... print(child.tag)
child1
child2
child3
可以使用insert()方法插入新的子節點:
>>> root.insert(0, etree.Element("child0"))
刪除子節點:
>>> root[0] = root[-1] # this moves the element!
>>> for child in root:
... print(child.tag)
child3
child1
child2
如果想把一個元素拷貝到不同的地方,需要創建一個獨立的deep 。
>>> from import deep
>>> element = etree.Element("neu")
>>> element.append( deep(root[1]) )
>>> print(element[0].tag)
child1
>>> print([ c.tag for c in root ])
[』child3』, 』child1』, 』child2』]
getparent()返回父節點:
>>> root is root[0].getparent() # lxml.etree only!
True
元素的兄弟或鄰居節點是通過next和previous屬性來訪問的
The siblings (or neighbours) of an element are accessed as next and previous elements:
>>> root[0] is root[1].getprevious() # lxml.etree only!
True
>>> root[1] is root[0].getnext() # lxml.etree only!
True
帶屬性的元素
XML元素支持屬性,可以用Element工廠方法直接創建。
>>> root = etree.Element("root", interesting="totally")
>>> etree.tostring(root)
b』<root interesting="totally"/>』
可以使用set和get方法訪問這些屬性:
>>> print root.get("interesting")
totally
>>> root.set("interesting", "somewhat")
>>> print root.get("interesting")
somewhat
也可以使用attrib性質的字典介面
>>> attributes = root.attrib
>>> print(attributes["interesting"])
somewhat
>>> print(attributes.get("hello"))
None
>>> attributes["hello"] = "Guten Tag"
>>> print(attributes.get("hello"))
Guten Tag
>>> print(root.get("hello"))
Guten Tag
元素可以包含文字:
>>> root = etree.Element("root")
>>> root.text = "TEXT"
>>> print(root.text)
TEXT
>>> etree.tostring(root)
』<root>TEXT</root>』
如果XML用在(X)HTML中,文本也可以在不同的元素中顯示:
<html><body>Hello<br/>World</body></html>
元素有tail屬性,它包含XML 樹中元素直接跟的,直到下個元素的文本。
>>> html = etree.Element("html")
>>> body = etree.SubElement(html, "body")
>>> body.text = "TEXT"
>>> etree.tostring(html)
b』<html><body>TEXT</body></html>』
>>> br = etree.SubElement(body, "br")
>>> etree.tostring(html)
b』<html><body>TEXT<br/></body></html>』
>>> br.tail = "TAIL"
>>> etree.tostring(html)
b』<html><body>TEXT<br/>TAIL</body></html>』
㈦ 你好,我要是想通過python的ElementTree的XML介面實現xml的刪除,修改,創建的功能該怎麼編寫。麻煩了。
from xml.etree.ElementTree import ElementTree
from lxml import etree ##的下載安裝lxml的包
Class XMLData:
def readXML(self,xml_file):##xml_file為xml文件名
doc = etree.ElementTree(file=xml_file)
root = doc.getroot();
items = root.getchildren();
listctrldata = {};
index = 1;
for item in items:
itemchild = item.getchildren();
arr_name = [];
fot ic in intemchild:
if ic.text = None:
ic.text=''
arr_name.append(ic.text)
listctrldata[index] = arr_name
index+=1
return listctrldata
##修改xml
def write(self,xml_file,listctrldata):
doc = etree.ElementTree(file=xml_file)
root = doc.getroot()
listlen = len(listctrldata)
itemlen = len(root.getchilddren())
if itemlen > listlen:
for more in range(itemlen-listlen):
root.remove(root.getchildren()[0])
elif itemlen < listlen:
for less in range(listlen-itemlen):
item = root.getchildren()[0]
itemadd = etree.Element(item.tag)
for it in item.getchildren():
itadd = stree.Element(it.tag)
itemadd.append(itadd)
root.append(itemadd)
else:pass
j=1
for item in root.getchildren():
itemchild = item.getchildren()
k = 0
for ic in itemchild:
ic.text = listctrldata[j][k]
k+=1
j+=1
f = open(xml_file,"w")
f.write(etree.tostring(root,pretty-print = true))
f.close()
㈧ 在python中用ElementTree提取XML中的內容
fromxml.etreeimportElementTree
str_=''#文件中的xml字元串
xml_obj=ElementTree.fromstring(str_)
然後通過對xml_obj進行操作,xml_obj本身也是一個xml節點回。
xml_obj.getchildren() 獲取根節點的子節點列表答
xml_obj.findall(node_name) 搜索xml_obj節點下名為node_name的所有節點
xml_obj.tag 節點的標簽
xml_obj.text 節點的文本信息 ,本例中可以獲得K這個文本。
xml_obj.tail 節點尾部的文本信息,本例中獲取Channel Regulator KCR1 Suppresses Heart Rhythm by Molating the Pacemaker Current I
就需要搜索到標簽為sup的節點,然後取節點的tail文本獲得。