<p>我有如下XML文件。</p>
<pre><code><?xml version="1.0" encoding="UTF-8"?><searching>
<query>query01</query>
<document id="0">
<title>lord of the rings.</title>
<snippet>
this is a snippet of a document.
</snippet>
<url>http://www.google.com/</url>
</document>
<document id="1">
<title>harry potter.</title>
<snippet>
this is a snippet of a document.
</snippet>
<url>http://www.google.com/</url>
</document>
........ #and other documents .....
<group id="0" size="298" score="145">
<title>
<phrase>GROUP A</phrase>
</title>
<document refid="0"/>
<document refid="1"/>
<document refid="84"/>
</group>
<group id="0" size="298" score="55">
<title>
<phrase>GROUP B</phrase>
</title>
<document refid="2"/>
<document refid="13"/>
<document refid="3"/>
</group>
</<searching>>
</code></pre>
<p>我想得到上面的组名以及每个组中的文档id(及其标题)是什么。我的想法是将文档id和文档标题存储到字典中,如下所示:</p>
<pre><code>import codecs
documentID = {}
group = {}
myfile = codecs.open("file.xml", mode = 'r', encoding = "utf8")
for line in myfile:
line = line.strip()
#get id from tags
#get title from tag
#store in documentID
#get group name and document reference
</code></pre>
<p>而且,我也试过美容组,但对它很陌生。我不知道该怎么办。这是我正在做的代码。</p>
<pre><code>def outputCluster(rFile):
documentInReadFile = {} #dictionary to store all document in readFile
myfile = codecs.open(rFile, mode='r', encoding="utf8")
soup = BeautifulSoup(myfile)
# print all text in readFile:
# print soup.prettify()
# print soup.find+_all('title')
outputCluster("file.xml")
</code></pre>
<p>请给我一些建议。
谢谢您。</p>