2017-02-10 46 views
0

我有一些XML:minidom命名 - 提取子節點

<sentence id="1086415:2"> 
<text>$6 and there is much tasty food, all of it fresh and continually refilled.</text> 
    <Opinions> 
    <Opinion to="31" from="27" polarity="positive" category="FOOD#STYLE_OPTIONS" target="food"/> 
    <Opinion to="31" from="27" polarity="positive" category="FOOD#QUALITY" target="food"/> 
    <Opinion to="31" from="27" polarity="positive" category="FOOD#PRICES" target="food"/> 
    </Opinions> 
</sentence> 
<sentence id="1086415:3"> 
<text>I am not a vegetarian but, almost all the dishes were great.</text> 
    <Opinions> 
    <Opinion to="48" from="42" polarity="positive" category="FOOD#QUALITY" target="dishes"/> 
    </Opinions> 

我試圖給意見標籤內的所有內容解壓到夫婦它與一個元組的文本。我想知道我怎麼可以用minidom來做到這一點?目前意見返回'\ n'。

from xml.dom import minidom 
xmldoc = minidom.parse("ABSA16_Restaurants_Train_SB1_v2.xml") 
sentences = xmldoc.getElementsByTagName("sentence") 
for sentence in sentences: 
    text = sentence.getElementsByTagName("text")[0].firstChild.data 
    opinion = sentence.getElementsByTagName("Opinions")[0].firstChild.data 

謝謝。

回答

0

您確定需要minidom

從文檔:

用戶誰是不是已經熟練的DOM應該用xml.etree.ElementTree模塊的XML處理 ,而不是考慮 。

沒有強烈的理由不浪費你的時間和使用標準的python xml.etree.ElementTree,它有足夠的例子在它的手動來解決你的任務。如果您有任何疑問,請隨時提問。

不僅如此,如果您需要經常使用XML,我建議第三方lxml,它是包含一些電池的更強大的工具。