0
我有以下的XML结构。(这是它的一部分)。实际上却TVEpisode”,‘TVShow’,‘电影’,‘TVSeries’,‘TVSeason’,我需要去通过XML文件并检查能解密元素。如果它不存在,需要根据上述类型的添加说明元件(mvoes,TVseries等)和插入影片,tvepisode等作为说明书的标题。添加新的元素到XML使用ElementTree的
<TVSeries>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<Description> The World's Fastest Indian </Description>
<SortTitle>World's Fastest Indian, The</SortTitle>
</TvSeries>
<Movies>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<Description> The World's Fastest Indian </Description>
<SortTitle>World's Fastest Indian, The</SortTitle>
</Movies>
<TVShow>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<SortTitle>World's Fastest Indian, The</SortTitle>
</TvShow>
在tvshow没有描述元素,所以我需要插入以下到:
<Description> The World's Fastest Indian </Description>
部分的XML文件:
<Feed xml:base="http://schemas.yyyy.com/xxxx/2011/06/13/ingestion" xmlns="http://schemas.yyy.com/xxxx/2011/06/13/ingestion">
<Movie>
<Provider>xxx2</Provider>
<Title>The World's Fastest Indian</Title>
<SortTitle>World's Fastest Indian, The</SortTitle>
</Movie>
<TVSeries>
<Provider>xxx</Provider>
<Title>The World's Fastest Indian</Title>
<Description> The World's Fastest Indian </Description>
<SortTitle>World's Fastest Indian, The</SortTitle>
</TvSeries>
我需要步行通过xml文件并且需要插入元素“description”,如果描述不存在(并且还需要在描述中添加一些文本)。
这是我done.this可以给我这没有说明的标题。但是,当我尝试插入元素的结构它给了我下面的错误:
File "/usr/lib/python2.4/site-packages/elementtree/ElementTree.py", line 293, in insert
assert iselement(element)
AssertionError
代码:
import elementtree.ElementTree as ET
import sys
import re
output_namespace='http://schemas.yyy.com/xxx/2011/06/13/ingestion'
types_to_remove=['TVEpisode','TVShow','Movie','TVSeries','TVSeason']
if ET.VERSION[0:3] == '1.2':
#in ET < 1.3, this is a workaround for supressing prefixes
def fixtag(tag, namespaces):
import string
# given a decorated tag (of the form {uri}tag), return prefixed
# tag and namespace declaration, if any
if isinstance(tag, ET.QName):
tag = tag.text
namespace_uri, tag = string.split(tag[1:], "}", 1)
prefix = namespaces.get(namespace_uri)
if namespace_uri not in namespaces:
prefix = ET._namespace_map.get(namespace_uri)
if namespace_uri not in ET._namespace_map:
prefix = "ns%d" % len(namespaces)
namespaces[namespace_uri] = prefix
if prefix == "xml":
xmlns = None
else:
if prefix is not None:
nsprefix = ':' + prefix
else:
nsprefix = ''
xmlns = ("xmlns%s" % nsprefix, namespace_uri)
else:
xmlns = None
if prefix is not None:
prefix += ":"
else:
prefix = ''
return "%s%s" % (prefix, tag), xmlns
ET.fixtag = fixtag
ET._namespace_map[output_namespace] = None
else:
#For ET > 1.3, use register_namespace function
ET.register_namespace('', output_namespace)
def descriptionAdd(root,type):
for child in root.findall('.//{http://schemas.yyy.com/xxx/2011/06/13/ingestion}%s' % type):
title=child.find('.//{http://schemas.yyy.com/xxx/2011/06/13/ingestion}Title').text
try:
if child.find('.//{http://schemas.yyy.com/xxx/2011/06/13 /ingestion}Description').text=="":
print("")
except:
print ' %s - couldn\'t find description' % (title)
print(child.tag)
child.insert(2,"Description")
####Do the actual work and writing new changes to the new xml file.
tree = ET.parse(sys.argv[1])
root = tree.getroot()
for type in types_to_remove:
descriptionAdd(root,type)
tree.write(sys.argv[2])
感谢您的回应,但它不工作,它给出以下error.AttributeError:'str'对象没有属性'makeelement' –
@Tharanga Abeyseela哎呀,对不起。这真是愚蠢的我。我把一个字符串放在其他错误的地方放一个字符串。我更新了答案。 –
@Tharanga Abeyseela再次更新。我让你的变量感到困惑。 –