Reputation: 2153
I have an metadata.xml file that has :
<MediaItem ActualEndTime="8/2/2017 5:05:01 PM" ActualStartTime="8/2/2017 4:00:01 PM" Clip="False" ClipEndTime="1/1/0001 12:00:00 AM" ClipParentId="00000000-0000-0000-0000-000000000000" ClipStartTime="1/1/0001 12:00:00 AM" CreatedDate="1/1/0001 12:00:00 AM" DateAdded="1/1/0001 12:00:00 AM" Duration="01:04:59.8290000" FileFormat="0" FileSize="0" Genre="Interview" GuideEndTime="8/2/2017 5:00:00 PM" GuideStartTime="8/2/2017 4:00:00 PM" MovieId="00000000-0000-0000-0000-000000000000" MovieYear="0" NumParts="0" OriginalAirDate="8/2/2017 12:00:00 AM" OriginalFileSize="3636725627" PartNum="0" RecordingType="Episodic" ShowSqueeze="True" TargetEndTime="8/2/2017 5:05:00 PM" TargetStartTime="8/2/2017 4:00:00 PM" UserId="" VideoHeight="0" VideoWidth="0" />
How do I parse it with ruby and nokogiri?
I've tried :
f = File.open path
puts "f : #{f}"
metadata = {}
doc = Nokogiri::XML(f)
puts "doc : #{doc}"
p = doc.xpath("//ActualStartTime")
puts "p : #{p}"
puts "++++++++++++++++++++++++++++++++++++++"
but I get this output :
f : #<File:0x00000002755c40>
doc : <?xml version="1.0"?>
p :
+++++++++++++++++++++++++++++++++++++++++++
What I am missing? I want to get ActualStartTime value, Duration value and Genre value.
update
metadata = {}
f = File.read(path)
puts "f : #{f}"
doc = Nokogiri::XML(f)
p = doc.xpath("//@ActualStartTime")
puts "p : #{p}"
metadata['Duration'] = doc.xpath("//MediaItem/@Duration")
puts "metadata['Duration'] : #{metadata['Duration']}"
output:
f : <MediaItem ActualEndTime="8/2/2017 5:05:01 PM" ActualStartTime="8/2/2017 4:00:01 PM" Clip="False" ClipEndTime="1/1/0001 12:00:00 AM" ClipParentId="00000000-0000-0000-0000-000000000000" ClipStartTime="1/1/0001 12:00:00 AM" CreatedDate="1/1/0001 12:00:00 AM" DateAdded="1/1/0001 12:00:00 AM" Duration="01:04:59.8290000" FileFormat="0" FileSize="0" Genre="Interview" GuideEndTime="8/2/2017 5:00:00 PM" GuideStartTime="8/2/2017 4:00:00 PM" MovieId="00000000-0000-0000-0000-000000000000" MovieYear="0" NumParts="0" OriginalAirDate="8/2/2017 12:00:00 AM" OriginalFileSize="3636725627" PartNum="0" RecordingType="Episodic" ShowSqueeze="True" TargetEndTime="8/2/2017 5:05:00 PM" TargetStartTime="8/2/2017 4:00:00 PM" UserId="" VideoHeight="0" VideoWidth="0" />
p :
metadata['Duration'] :
Upvotes: 0
Views: 66
Reputation: 11035
You're opening the file, but you're never actually reading the file contents (and also not closing the file after). You can read the file through
begin
file = File.open './metadata.xml'
f = file.read
ensure
file.close
end
or through one of the shortcuts:
# closes file after block finishes
f = File.open('./metadata.xml') { |f| f.read }
or, since reading is such a common thing, you can use File.read
f = File.read('./metadata.xml')
and now Nokogiri will read f
just fine, though you'll need to use
p = doc.xpath("//@ActualStartTime")
since it's an attribute.
Upvotes: 3