Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

Trying to parse an RSS feed in Ruby but the XML notation for the feed has colons (:) in them. How should I define these in Ruby?

Here is my problem, the feed that I am trying to reproduce has the colon symbol in the actual item variable in the XML notation. In ruby I am trying to define these variables, but I assume having the colon symbol in there will cause problems. Here is the XML notation:

<rss version="2.0">
<channel>
<title>NASDAQTrader.com</title>
<link>http://www.nasdaqtrader.com</link>
<description>NASDAQ Trade Halts</description>
<copyright>Copyright 2021. All rights reserved.</copyright>
<pubDate>Fri, 22 Jan 2021 10:40:41 GMT</pubDate>
<ttl>1</ttl>
<ndaq:numItems>3</ndaq:numItems>
<item>
<title>PRVL</title>
<pubDate>Thu, 21 Jan 2021 05:00:00 GMT</pubDate>
<ndaq:HaltDate>01/21/2021</ndaq:HaltDate>
<ndaq:HaltTime>19:45:17</ndaq:HaltTime>
<ndaq:IssueSymbol>PRVL</ndaq:IssueSymbol>
<ndaq:IssueName>Prevail Therapeutics Inc. Cmn</ndaq:IssueName>
<ndaq:Market>NASDAQ</ndaq:Market>
<ndaq:ReasonCode>T12</ndaq:ReasonCode>
<ndaq:PauseThresholdPrice/>
<ndaq:ResumptionDate/>
<ndaq:ResumptionQuoteTime/>
<ndaq:ResumptionTradeTime/>
<description>
</description>
</item>

Should I just use the second part of the variable name? For example instead of calling it "ndaq:HaltDate" just call it "HaltDate"?

I apologize if this sounds like a dumb question, I am new to Ruby but not new to programming; that's why this threw up a red flag when I started writing my code.

question from:https://stackoverflow.com/questions/65845673/trying-to-parse-an-rss-feed-in-ruby-but-the-xml-notation-for-the-feed-has-colons

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ndaq namespace is not defined which causes REXML to not parse it, but otherwise I don't see a problem with ruby. Colons in dictionary keys is not an issue.

This simple script shows one way that your xml can be displayed in a Ruby Hash:

require 'nori'
require 'pp'

response_hash = Nori.new.parse(DATA.read)

pp response_hash

__END__
<rss version="2.0">
<channel>
<title>NASDAQTrader.com</title>
<link>http://www.nasdaqtrader.com</link>
<description>NASDAQ Trade Halts</description>
<copyright>Copyright 2021. All rights reserved.</copyright>
<pubDate>Fri, 22 Jan 2021 10:40:41 GMT</pubDate>
<ttl>1</ttl>
<ndaq:numItems>3</ndaq:numItems>
<item>
<title>PRVL</title>
<pubDate>Thu, 21 Jan 2021 05:00:00 GMT</pubDate>
<ndaq:HaltDate>01/21/2021</ndaq:HaltDate>
<ndaq:HaltTime>19:45:17</ndaq:HaltTime>
<ndaq:IssueSymbol>PRVL</ndaq:IssueSymbol>
<ndaq:IssueName>Prevail Therapeutics Inc. Cmn</ndaq:IssueName>
<ndaq:Market>NASDAQ</ndaq:Market>
<ndaq:ReasonCode>T12</ndaq:ReasonCode>
<ndaq:PauseThresholdPrice/>
<ndaq:ResumptionDate/>
<ndaq:ResumptionQuoteTime/>
<ndaq:ResumptionTradeTime/>
<description>
</description>
</item>

The output of this script is:

{"channel"=>
  {"title"=>"NASDAQTrader.com",
   "link"=>"http://www.nasdaqtrader.com",
   "description"=>"NASDAQ Trade Halts",
   "copyright"=>"Copyright 2021. All rights reserved.",
   "pubDate"=>"Fri, 22 Jan 2021 10:40:41 GMT",
   "ttl"=>"1",
   "ndaq:numItems"=>"3",
   "item"=>
    {"title"=>"PRVL",
     "pubDate"=>"Thu, 21 Jan 2021 05:00:00 GMT",
     "ndaq:HaltDate"=>"01/21/2021",
     "ndaq:HaltTime"=>2021-01-22 19:45:17 +0100,
     "ndaq:IssueSymbol"=>"PRVL",
     "ndaq:IssueName"=>"Prevail Therapeutics Inc. Cmn",
     "ndaq:Market"=>"NASDAQ",
     "ndaq:ReasonCode"=>"T12",
     "ndaq:PauseThresholdPrice"=>nil,
     "ndaq:ResumptionDate"=>nil,
     "ndaq:ResumptionQuoteTime"=>nil,
     "ndaq:ResumptionTradeTime"=>nil,
     "description"=>nil}}}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...