2016-07-19 2 views
0

巨大なXMLファイルからいくつかの特定のフィールドを抽出しようとしています。lxmlタグを無視するspcificタグの間に来る

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE dblp SYSTEM "dblp.dtd"> 
    <dblp> 

<article mdate="2009-09-24" key="journals/jasis/GianoliM09"> 
<author>Ernesto Gianoli</author> 
<author>Marco A. Molina-Montenegro</author> 
<title>Insights into the relationship between the <i>h</i>-index and self-citations.</title> 
<pages>1283-1285</pages> 
<year>2009</year> 
<volume>60</volume> 
<journal>JASIST</journal> 
<number>6</number> 
<ee>http://dx.doi.org/10.1002/asi.21042</ee> 
<url>db/journals/jasis/jasis60.html#GianoliM09</url> 
</article> 


<article mdate="2014-09-18" key="journals/iacr/ShiCSL11" publtype="informal publication"> 
<author>Elaine Shi</author> 
<author>T.-H. Hubert Chan</author> 
<author>Emil Stefanov</author> 
<author>Mingfei Li</author> 
<title>blivious RAM with O((log N)<sup>3</sup>) Worst-Case Cost.</title> 
<pages>407</pages> 
<year>2011</year> 
<volume>2011</volume> 
<journal>IACR Cryptology ePrint Archive</journal> 
<ee>http://eprint.iacr.org/2011/407</ee> 
<url>db/journals/iacr/iacr2011.html#ShiCSL11</url> 
</article> 

<phdthesis mdate="2016-05-04" key="phd/it/Popescu2008"> 
<author>Razvan Andrei Popescu</author> 
<title>Aggregation and adaptation of web services: a semi-automated methodology for the aggregation and adaption of web services.</title> 
<year>2008</year> 
<school>University of Pisa</school> 
<pages>1-206</pages> 
<isbn>978-3-8364-6280-8</isbn> 
<ee>http://d-nb.info/991165179</ee> 
</phdthesis><phdthesis mdate="2007-04-26" key="phd/Tsangaris92"> 
<author>Manolis M. Tsangaris</author> 
<title>Principles of Static Clustering for Object Oriented Databases</title> 
<year>1992</year> 
<school>Univ. of Wisconsin-Madison</school> 
</phdthesis> 

<phdthesis mdate="2005-11-30" key="phd/Heuer2002"> 
<author>Andreas Heuer 0002</author> 
<title>Web-Pr&auml;senz-Management im Unternehmen</title> 
<year>2002</year> 
<school>Univ. Trier, FB 4, Informatik</school> 
<ee>http://ubt.opus.hbz-nrw.de/volltexte/2004/144/</ee> 
</phdthesis> 

<mastersthesis mdate="2002-01-03" key="phd/Schulte92"> 
<author>Christian Schulte</author> 
<title>Entwurf und Implementierung eines &uuml;bersetzenden Systems f&uuml;r das intuitionistische logische Programmieren auf der Warren Abstract Machine.</title> 
<year>1991</year> 
<school>Universit&auml;t Karlsruhe, Institut f&uuml;r Logik, Komplexit&auml;t und Deduktionssysteme</school> 
</mastersthesis> 

<phdthesis mdate="2002-01-03" key="phd/Hellerstein95"> 
<author>Joseph M. Hellerstein</author> 
<title>Optimization and Execution Techniques for Queries With Expensive Methods</title> 
<year>1995</year> 
<school>Univ. of Wisconsin-Madison</school> 
</phdthesis> 

</dblp> 

と私は私が興味フィールドを解析して抽出するためのコードhereを使用し、私は最初のケースでタイトルを抽出したいときに問題が生じると第2ケースので:ここでの例です。 <i>h</i><sup>3</sup>タグのパーサは新しいタグを満たすまで、基本的に私はタイトルテキストを取得

title Insights into the relationship between the 
blivious RAM with O((log N) 

:私のコードは、新しいイベントとしてではなく<title>タグの一部としてそれらを見ると、私は次のような結果を得るようです。

問題はいくつかのケース(例:異なるタグ)があるかどうかわかりません。そうでなければ手動で削除しようとする可能性があります。そのような事件を処理するにはどうしますか?

答えて

1

要素コンテンツ(特にtailプロパティ)のlxmlデータモデルに注意する必要があります。これはここでよく説明されています:http://infohost.nmt.edu/tcc/help/pubs/pylxml/web/etree-view.html

この要素のtextのプロパティの内容、

<title>Insights into the relationship between the <i>h</i>-index and self-citations.</title> 

Insights into the relationship between theあります。

hビットは、<i>子要素のtext-index and self-citations.は同じ子のtailあります。


タイトルのすべてのテキストコンテンツを取得するには、itertext()を使用できます。例:

from lxml import etree 

tree = etree.parse("dblp.xml") # The XML in the question 
titles = tree.xpath("//title") 

for title in titles: 
    print ''.join(title.itertext()) 

出力:

Insights into the relationship between the h-index and self-citations. 
blivious RAM with O((log N)3) Worst-Case Cost. 
Aggregation and adaptation of web services: a semi-automated methodology for the aggregation and adaption of web services. 
Principles of Static Clustering for Object Oriented Databases 
Web-Präsenz-Management im Unternehmen 
Entwurf und Implementierung eines übersetzenden Systems für das intuitionistische logische Programmieren auf der Warren Abstract Machine. 
Optimization and Execution Techniques for Queries With Expensive Methods 
関連する問題