2015-09-01 56 views
-1

我想以這種格式將數據寫入到一個文本文件的標題和描述數據:如何從XML文件

Channel|Date|Start time|Duration|Title|Desc 

下面是XML例子:

<!-- language: xml --> 
<tv> 
    <channel id="YLE TV1"> 
    <display-name lang="fi">YLE TV1</display-name> 
    <programme start="20150828110000 +0300" stop="20150828110500 +0300" >channel="YLE TV1"> 
    <title lang="fi">Yle Uutiset</title> 
    </programme> 
    <programme start="20150828110500 +0300" stop="20150828111200 +0300" >channel="YLE TV1"> 
    <title lang="fi">Yle Uutiset Uusimaa</title> 
    <desc lang="fi">Uutisia Uudeltamaalta.(n)</desc> 
    </programme> 
</tv> 

這裏是我的Ruby代碼:

require 'rubygems' 
require 'nokogiri' 

open('myfile.out', 'a') do |f| 
    doc = Nokogiri::XML(File.open("guidetv1.xml")) 
    doc.css("programme").each do |response_node| 

    strChannel = response_node["channel"] 
    if(strChannel.eql? "YLE TV1") 
     strChannel = "1" 
    elsif(strChannel.eql? "YLE TV2") 
     strChannel = "2" 
    end 
    strStart = response_node["start"] 
    strStop = response_node["stop"] 
    strTitle = response_node["title"] 
    strDesc = response_node["desc"] 
    f.puts strChannel + "|" + strStart + "|" + strStop + "|" + strTitle + "|" + strDesc 
    end 
end 

我如何讀titledesc數據? 如何檢查是否存在Desc

+0

不幸的是,由於格式錯誤的XML示例,您的帖子不可讀。請檢查[Markdown幫助](http://stackoverflow.com/editing-help)並確保在提交格式錯誤後查看帖子。 – XLII

+0

您的「文本文件」是CSV(「逗號分隔值」)文件的變體。看看Ruby的CSV類,並找出如何告訴它使用'|'而不是字段之間的逗號。它會處理你沒有想到的各種角落案例。 –

+0

什麼是「String strChannel」和類似的行?這不是Ruby。 –

回答

0

這是未經過測試的代碼。調整XML以使其在語法上正確。閱讀CSVDate/DateTimeTime文檔以完成標記的代碼。

require 'csv' 
require 'nokogiri' 

doc = Nokogiri::XML(<<EOT) 
<tv> 
    <channel id="YLE TV1"> 
    <display-name lang="fi">YLE TV1</display-name> 
    <programme start="20150828110000 +0300" stop="20150828110500 +0300" channel="YLE TV1"> 
    <title lang="fi">Yle Uutiset</title> 
    </programme> 
    <programme start="20150828110500 +0300" stop="20150828111200 +0300" channel="YLE TV1"> 
    <title lang="fi">Yle Uutiset Uusimaa</title> 
    <desc lang="fi">Uutisia Uudeltamaalta.(n)</desc> 
    </programme> 
</tv> 
EOT 

CSV.open(
    'file.csv', 'wb', 
    quote_char: '|', 
    headers: ['Channel', 'Date', 'Start time', 'Duration', 'Title', 'Desc'] 
) do |csv| 
    doc.search('programme').each do |programme| 
    channel = programme['channel'] 
    date = programme['start']  # => further processing needed 
    start_time = programme['start'] # => further processing needed 
    stop_time = programme['stop'] # => further processing needed 
    duration = 0     # => further processing needed 
    title = programme.at('title').text 
    desc = programme.at('desc').text || '' 

    csv << [channel, date, start_time, duration, title, desc] 
    end 
end