博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python:ElementTree操作XML
阅读量:6969 次
发布时间:2019-06-27

本文共 8305 字,大约阅读时间需要 27 分钟。

http://www.python.org/doc/current/library/xml.etree.elementtree.html

 

 

 

1.引入库 

需要用到3个类,ElementTree,Element以及建立子类的包装类SubElement  
from xml.etree.ElementTree import ElementTree 
from xml.etree.ElementTree import Element 
from xml.etree.ElementTree import SubElement as SE

2.读入并解析 

    tree = ElementTree(file=xmlfile) 
    root = tree.getroot() 
   读入后,tree是ElementTree的类型,获取xml根结点使用getroot()方法; 

XML示例文件: 

<item sid=’1712′ name = ‘大CC’  > 
<a id=1></a> 
<a id=2></a> 
</item>

3.获取儿子结点 

查找Element的所有子结点: 
AArry = item.findall(‘a’) 
也可使用getchildren(): 
childs =  item.getchildren() 
     for subItem in childs: 
           print subItem.get(‘id’)

4.插入儿子结点 

方法一: 
    item = Element("item", {’sid’ : ‘1713′, ‘name’ : ‘ityouhui’}) 
    root.append(item) 
方法二: 
    SE(root,’item’,{’sid’:'1713′,’name’:'ityouhui’}) 
法一的好处是插入之后可以对item继续操作。法二是写法上简单,其中SE就是SubElement,在引入处做了声明;

5.操作属性 

获取Element的某个属性值(eg:获取item的 name) 
print root.find(‘item/name’).text 
print item.get(‘name’) 
获取Element所有属性 
       print item.items()       # [('sid', '1712'), ('name', '大CC')] 
       print item.attrib        # {’sid’: ‘1712′, ‘name’: ‘大CC’}

6.美化XML 

在写入之前,传入root调用此函数,写入的XML文件格式整齐美观: 
    indent(root) 
    book.write(xmlfile,’utf-8′)

 

## Get pretty lookdef indent( elem, level=0):    i = "\n" + level*"  "    if len(elem):        if not elem.text or not elem.text.strip():            elem.text = i + "  "        for e in elem:            indent(e, level+1)        if not e.tail or not e.tail.strip():            e.tail = i    if level and (not elem.tail or not elem.tail.strip()):        elem.tail = i    return elem
[python] 
 
  1. try:  
  2.     tree = ET.parse("Campaign_Config.xml"#打开xml文档  
  3.     root = tree.getroot() #获得root节点  
  4. except Exception, e:  
  5.     print "Error: cannot parse file: Campaign_Config.xml."  
  6.     return -1     
  7. eles_Paramter = root.find("Parameters").findall("Parameter"#找到Parameters节点下的所有Parameter节点  
  8. for eachEle in eles_Paramter:  
  9.     if eachEle.attrib.has_key("isControlled"): #如果有个节点拥有属性isControlledPSUsed  
  10.         eachEle.set("isControlled""False")    #就将该属性设置为False  
  11. tree.write("Campaign_Config.xml")   #最后将结果写回文件

 

# 加载XML文件(2种方法,一是加载指定字符串,二是加载指定文件) 

# root = ElementTree.parse(r"D:\test.xml") 
root = ElementTree.fromstring(text)

 

Element具有的属性和方法:

tag
A string identifying what kind of data this element represents (the element type, in other words).
text
The 
text attribute can be used to hold additional data associated with the element. As the name implies this attribute is usually a string but may be any application-specific object. If the element is created from an XML file the attribute will contain any text found between the element tags.
tail
The 
tail attribute can be used to hold additional data associated with the element. This attribute is usually a string but may be any application-specific object. If the element is created from an XML file the attribute will contain any text found after the element’s end tag and before the next tag.
attrib
A dictionary containing the element’s attributes. Note that while the 
attrib value is always a real mutable Python dictionary, an ElementTree implementation may choose to use another internal representation, and create the dictionary only if someone asks for it. To take advantage of such implementations, use the dictionary methods below whenever possible.

 

The following dictionary-like methods work on the element attributes.

clear
(
)
Resets an element. This function removes all subelements, clears all attributes, and sets the text and tail attributes to None.
get
(
key,
default=None
)

Gets the element attribute named key.

Returns the attribute value, or default if the attribute was not found.

items
(
)
Returns the element attributes as a sequence of (name, value) pairs. The attributes are returned in an arbitrary order.
keys
(
)
Returns the elements attribute names as a list. The names are returned in an arbitrary order.
set
(
key,
value
)
Set the attribute 
key on the element to 
value.

The following methods work on the element’s children (subelements).

append
(
subelement
)
Adds the element 
subelement to the end of this elements internal list of subelements.
extend
(
subelements
)

Appends subelements from a sequence object with zero or more elements. Raises if a subelement is not a valid object.

New in version 2.7.

find
(
match
)
Finds the first subelement matching 
match
match may be a tag name or path. Returns an element instance or
None.
findall
(
match
)
Finds all matching subelements, by tag name or path. Returns a list containing all matching elements in document order.
findtext
(
match,
default=None
)
Finds text for the first subelement matching 
match
match may be a tag name or path. Returns the text content of the first matching element, or
default if no element was found. Note that if the matching element has no text content an empty string is returned.
getchildren
(
)

Deprecated since version 2.7:Uselist(elem) or iteration.

getiterator
(
tag=None
)

Deprecated since version 2.7:Use method instead.

insert
(
index,
element
)
Inserts a subelement at the given position in this element.
iter
(
tag=None
)
Creates a tree   with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. If 
tag is not 
None or 
'*', only elements whose tag equals
tag are returned from the iterator. If the tree structure is modified during iteration, the result is undefined.
iterfind
(
match
)

Finds all matching subelements, by tag name or path. Returns an iterable yielding all matching elements in document order.

New in version 2.7.

itertext
(
)

Creates a text iterator. The iterator loops over this element and all subelements, in document order, and returns all inner text.

New in version 2.7.

makeelement
(
tag,
attrib
)
Creates a new element object of the same type as this element. Do not call this method, use the  factory function instead.
remove
(
subelement
)
Removes 
subelement from the element. Unlike the find* methods this method compares elements based on the instance identity, not on tag value or contents.

ElementTree具有的属性和方法:

 

_setroot
(
element
)
Replaces the root element for this tree. This discards the current contents of the tree, and replaces it with the given element. Use with care.
element is an element instance.
find
(
match
)
Finds the first toplevel element matching 
match
match may be a tag name or path. Same as getroot().find(match). Returns the first matching element, or
None if no element was found.
findall
(
match
)
Finds all matching subelements, by tag name or path. Same as getroot().findall(match).
match may be a tag name or path. Returns a list containing all matching elements, in document order.
findtext
(
match,
default=None
)
Finds the element text for the first toplevel element with given tag. Same as getroot().findtext(match).
matchmay be a tag name or path.
default is the value to return if the element was not found. Returns the text content of the first matching element, or the default value no element was found. Note that if the element is found, but has no text content, this method returns an empty string.
getiterator
(
tag=None
)

Deprecated since version 2.7:Use method instead.

getroot
(
)
Returns the root element for this tree.
iter
(
tag=None
)
Creates and returns a tree iterator for the root element. The iterator loops over all elements in this tree, in section order.
tag is the tag to look for (default is to return all elements)
iterfind
(
match
)

Finds all matching subelements, by tag name or path. Same as getroot().iterfind(match). Returns an iterable yielding all matching elements in document order.

New in version 2.7.

parse
(
source,
parser=None
)
Loads an external XML section into this element tree. 
source is a file name or file object.
parser is an optional parser instance. If not given, the standard XMLParser parser is used. Returns the section root element.
write
(
file,
encoding="us-ascii",
xml_declaration=None,
method="xml"
)
Writes the element tree to a file, as XML. 
file is a file name, or a file object opened for writing.
encoding  is the output encoding (default is US-ASCII).
xml_declaration controls if an XML declaration should be added to the file. Use False for never, True for always, None for only if not US-ASCII or UTF-8 (default is None).
method is either
"xml",
"html" or
"text" (default is
"xml"). Returns an encoded string.

 

转载于:https://www.cnblogs.com/viviancc/archive/2013/06/07/3123845.html

你可能感兴趣的文章
.net微信公众号开发——群发消息
查看>>
纪念逝去的岁月——C++实现一个队列(使用类模板)
查看>>
GO语言练习:多返回值函数
查看>>
让x86的android模拟器能模拟arm架构系统
查看>>
初学Struts2-自定义拦截器及其配置
查看>>
关于js中的几个小问题。
查看>>
hdoj-2058-the sum problem
查看>>
MySql基础整理
查看>>
Spring Bean Scope 有状态的Bean 无状态的Bean
查看>>
php 批量修改文件格式或重命名
查看>>
Android数据加密之Aes加密
查看>>
InputStream,String相互转化
查看>>
Atitit.gui api自动化调用技术原理与实践
查看>>
详解zabbix安装部署(Server端篇)
查看>>
阿里云负载不支持 WebSocket 协议与 WSS 和 Nginx 配置问题
查看>>
获取Android屏幕尺寸、控件尺寸、状态栏/通知栏高度、导航栏高度
查看>>
Android开之在非UI线程中更新UI
查看>>
redis分布式锁小试
查看>>
007 爬虫(Scrapy库的使用)
查看>>
014——VUE中v-if语法在网站注册中的实际应用
查看>>