How can Nokogiri extract metadata and combine with main record

25 views
Skip to first unread message

Greg S

unread,
Feb 24, 2022, 5:23:02 PM2/24/22
to nokogiri-talk
200,000 line XML and the data of interest looks like the following (extraneous info removed). There are other Records and nodes not of interest. This is in a Rails site.

    <Record type="HKQuantityTypeIdentifier" startDate="2021-10-05 09:43:40 -0800" value="130">
      <MetadataEntry key="HKTimeZone" value="America/Los_Angeles"/>
      other MetadatEntries
    </Record>

The following Nokogiri grabs only the top lines of each `Record` to `records`

    document = File.open(path) { |f| Nokogiri::XML(f) }
    records = document.xpath("//Record[contains(@type,'HKQuantityTypeIdentifier')]").map(&:to_h)
   
with a typical line looking like:

    {"type"=>"HKQuantityTypeIdentifier", "startDate"=>"2014-04-02 09:48:00 -0800", "value"=>"110"}
   
 
I want to add the `HKTimeZone` MetadataEntry to the hash (and then I can extract the information together later)

    {"type"=>"HKQuantityTypeIdentifier", "startDate"=>"2014-04-02 09:48:00 -0800", "value"=>"110", timeZone="America/Los_Angeles"}
   
or whatever the value was for `HKTimeZone`. The `startDate`, `value` and `timeZone` are grabbed line by line and added to the database along with other data.

Can Nokogiri do this? Or any other suggestions? Can't say as I understand Nokogiri. Most of the code is by someone other than me. Thank you.

Or can Nokogiri, just add the entire `Record` to `record` and then parse it later? In other words Nogogiri gathers each Record with `type="HKQuantityTypeIdentifier"` and parse afterwards.

I tried Stackoverflow and got no response, so I may be offbase with how to use Nokogiri.
Reply all
Reply to author
Forward
0 new messages