Skip to content

Lesson: Parse an Existing XML File with a Terminology

acozine edited this page May 7, 2014 · 3 revisions

This Tutorial is known to work with om version 3.0.4.
Please update this wiki to reflect any other versions that have been tested.

Goals

  • Define Terms in a Terminology that refer to XML elements with specific attribute values

Explanation

Steps

Step 1: Select an XML file to Parse

We will re-use the Terminology defined in fancy_book_metadata.rb from the previous lessons. By the end of Lesson: Make your Terminology aware of XML Namespaces we had set up the FancyBookMetadata terminology to use the mods namespace.

We will also re-use the xml file we created called funny_sample.xml, which we updated to include the mods namespace declaration.

Step 2: Parse the XML file with the Terminology you Defined

Open the console and require the FancyBookMetadata class definition.

bundle console
require "./fancy_book_metadata"

Open the file and parse it as a FancyBookMetadata Document.

file = File.new("funny_sample.xml")
funnysample = FancyBookMetadata.from_xml(file)
funnysample.name.count
 => 2
funnysample.name.family_name
=> ["Horn", "Caesar"] 
funnysample.name(0).given_name
=> ["Zoia"] 

Next Step

Return to the Tame your XML with OM page.