# Reading XML to relational tables

**URL:** <https://www.ruby-forum.com/t/reading-xml-to-relational-tables/205260>\
**Category:** Ruby\
**Created:** [April 2, 2011, 12:47am UTC](https://www.ruby-forum.com/t/reading-xml-to-relational-tables/205260 "2011-04-02T00:47:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [April 2, 2011, 12:47am UTC](https://www.ruby-forum.com/t/reading-xml-to-relational-tables/205260/1 "2011-04-02T00:47:20Z")

</div>

Hi everyone,

I need to build 3 relational tables from an xml text. In this tables, I  
need to keep track of words that have the and tags in them  
along with the  
word mentioned and its count in the

tag. This is easier to  
illustrate with an example:

I need to take this text:

My name is **Ted** , and I like coffee. **Ted** does not like tea.

I have a brother who likes tea but does not like coffee

To 3 normalized tables like this:

…p\_table…  
p\_id desc  
1 My name is…  
2 I have a …

…p\_to\_emph\_table…  
p\_id e\_id count  
1 2 1  
2 1 1  
2 2 1

…emph\_table…  
e\_id emph\_word  
1 Tea  
2 Coffee

I am not sure what would be the best approach to parse this xml with  
ruby or what tool  
could help me do this efficiently?

Any ideas appreciated,

Ted.

---

<div class="post-metadata">

**Author:** ![tedforte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tedforte](https://www.ruby-forum.com/u/tedforte)\
**Post date:** [April 2, 2011, 1:02am UTC](https://www.ruby-forum.com/t/reading-xml-to-relational-tables/205260/2 "2011-04-02T01:02:38Z")

</div>

On Sat, Apr 2, 2011 at 12:47 AM, Ted F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> My name is **Ted** , and I like coffee.
> 
> 1 Tea  
> 2 Coffee
> 
> I am not sure what would be the best approach to parse this xml with  
> ruby or what tool  
> could help me do this efficiently?

What I’d do is parse the XML (use Nokogiri, for example) and get all p  
elements. For each p element, insert it into p\_table if not present  
and get its id. Look at all emph inside the p element, and for each of  
them:

- Check if the word is already in emph\_table and get the id or
- Insert it into emph\_table and get the id

With that id, insert or update a row in the p\_to\_emph\_table with the p  
and the word id.

This is a straightforward approach that should work. Make a try (ask  
any question that blocks you) and let us know how it goes.

Jesus.

---

<div class="post-metadata">

**Author:** ![tedforte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tedforte](https://www.ruby-forum.com/u/tedforte)\
**Post date:** [April 9, 2011, 11:39pm UTC](https://www.ruby-forum.com/t/reading-xml-to-relational-tables/205260/3 "2011-04-09T23:39:55Z")

</div>

Hi Jesus,

Thank you for your help. Right now I am stuck trying to traverse the  
elements in a single xml::element. I know I can use this elements method  
to list the elements, but I am not sure how  
I can traverse through them and get their contents individually.

xml = File.read(‘translateXML.xml’)  
doc = Nokogiri::XML(xml)

# split into sentences first

arr = doc.search(‘p’)

puts arr[0].elements

---

<div class="post-metadata">

**Author:** ![tedforte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tedforte](https://www.ruby-forum.com/u/tedforte)\
**Post date:** [April 11, 2011, 9:57am UTC](https://www.ruby-forum.com/t/reading-xml-to-relational-tables/205260/4 "2011-04-11T09:57:19Z")

</div>

On Sat, Apr 9, 2011 at 11:39 PM, Ted F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> # split into sentences first
> 
> arr = doc.search(‘p’)

Try something like:

require ‘nokogiri’

doc = Nokogiri::XML(File.read(“p.xml”))  
doc.search(“p”).each do |p\_element|  
puts “---------”  
puts p\_element.text  
p\_element.css(“emph,strong”).each do |emph|  
puts “Highlighted: #{emph.text}”  
end  
end

Jesus.
