2012-04-18 26 views
1

我使用phpmyadmin將一個mysql數據庫導出到了xml,現在我想用minidom解析它,但是我無法獲取需要的形式的內容。使用xml.dom.minidom從Mysql XML轉儲提取數據

摘要:我需要分配變量title包含內<column name="news_title">This is the title</column>

文本提取分貝是這樣的:

<pma_xml_export version="1.0" > 
    <database name="dbname"> 
     <!-- Table newsbox --> 
     <table name="newsbox"> 
      <column name="news_id">1</column> 
      <column name="news_title">This is the title</column> 
      <column name="news_text">This is the news text</column> 
      <column name="date">Thu, 28 Feb 2008 20:10:30 -0500</column> 
      <column name="author">author</column> 
      <column name="category">site_announcement</column> 
     </table> 
    </database> 
</pma_xml_export> 

我可以用下面的腳本來提取文本,但它不是在形式,我需要:

​​

我需要的是什麼,我可以將文本conten這些元素變量,可以在例如傳遞的TS,

for item in columns: 
    title = ??? 
    text = ??? 
    date = ??? 
    author = ??? 

如果分貝產量在<title>Here's the Title</title>形式我會有大量的實例去了,但我無法找到任何參考像<column name="news_title">This is the title</column>

回答

1

這已經有一段時間,因爲我用xml.dom.minidom但這應該工作...

columns = [c.firstChild.data for c in pmaexport.getElementsByTagName('column') if c.getAttribute('name') == 'news_title'] 

加一樣,列表理解!