# Parse XML to database

**URL:** <https://rubytalk.org/t/parse-xml-to-database/46853>\
**Category:** ruby-talk\
**Created:** [2 June 2008 02:06 UTC](https://rubytalk.org/t/parse-xml-to-database/46853 "2008-06-02T02:06:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cean\_Ooi](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Cean\_Ooi](https://rubytalk.org/u/Cean_Ooi)\
**Post date:** [2 June 2008 02:06 UTC](https://rubytalk.org/t/parse-xml-to-database/46853/1 "2008-06-02T02:06:39Z")

</div>

hi, can someone help me with this  
after i read theres nothing show in my database.... wat happen...?  
where is my error in my code??

class Map

&nbsp;&nbsp;require 'rexml/document'  
require "mysql"  
require "dbi"  
include REXML  
scanfile = File.new('River\_Va.lmx')  
doc = doc = File.open('River\_Va.lmx', 'rb') {|scanfile|  
Document.new(scanfile)  
}  
puts doc

maps = REXML::Document.new()  
&nbsp;&nbsp;&nbsp;&nbsp;root = maps.root

names = []  
&nbsp;&nbsp;&nbsp;&nbsp;latitudes = []  
&nbsp;&nbsp;&nbsp;&nbsp;longtitudes = []

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~ MSISDN = {}  
&nbsp;&nbsp;&nbsp;&nbsp;#~ mobils.elements.each("MobileDevices/MobileDevice") { |element|  
puts  
#~ element.attributes["MSISDN"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~ MSISDN = element.attributes["MSISDN"] }

&nbsp;&nbsp;&nbsp;&nbsp;#~ datetime = {}  
&nbsp;&nbsp;&nbsp;&nbsp;#~ mobils.elements.each("MobileDevices/MobileDevice") { |element|  
puts  
#~ element.attributes["datetime"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~ datetime = element.attributes["datetime"] }

&nbsp;&nbsp;&nbsp;&nbsp;#~ description = {}  
&nbsp;&nbsp;&nbsp;&nbsp;#~ mobils.elements.each("MobileDevices/MobileDevice") { |element|  
puts  
#~ element.attributes["description"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~ description = element.attributes["description"] }

&nbsp;&nbsp;&nbsp;&nbsp;maps.elements.each("lm:landmark") { |element| puts  
element.attributes["name"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;names.push element.attributes["name"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name = element.attributes["name"]}

puts name  
&nbsp;&nbsp;&nbsp;&nbsp;maps.elements.each("lm:landmark") { |element| puts  
element.attributes["latitude"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;latitudes.push element.attributes["latitude"]}

&nbsp;&nbsp;puts latitudes  
&nbsp;&nbsp;&nbsp;&nbsp;maps.elements.each("lm:longitude") { |element| puts  
element.attributes["longtitude"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;longtitudes.push element.attributes["longtitude"] }

puts longtitudes

# db insert  
&nbsp;&nbsp;&nbsp;dbname="email\_development"

&nbsp;&nbsp;m = Mysql.new("localhost", "root", "", "email\_development")  
&nbsp;&nbsp;sth=m.query("insert into maps (name,latitude,longtitude) values  
(name,latitude,longtitude)")  
&nbsp;&nbsp;&nbsp;&nbsp;#~ ("INSERT INTO Maps (name, latitude,  
#~ longtitude)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~ VALUES (?,?,?,?,?)")  
&nbsp;&nbsp;&nbsp;&nbsp;#~ models.each\_index do |index|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~name = names[index]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~ latitude = latitudes[index]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#~ longtitude = longtitudes[index]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# sth.execute("River\_Va.lmx", "email\_development", "#{names}",  
#"#{latitudes}", "#{longtitudes}")  
#end

end

this is my xml file~~

\<?xml version="1.0" encoding="UTF-8"?\>  
\<lm:lmx xmlns:lm="[http://www.nokia.com/schemas/location/landmarks/1/0](http://www.nokia.com/schemas/location/landmarks/1/0)"  
xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
xsi:schemaLocation="[http://www.nokia.com/schemas/location/landmarks/1/0](http://www.nokia.com/schemas/location/landmarks/1/0)  
lmx.xsd"\>  
\<lm:landmarkCollection\>  
\<lm:landmark\>  
\<lm:name\>River Valley High School\</lm:name\>  
\<lm:coordinates\>  
\<lm:latitude\>9.36\</lm:latitude\>  
\<lm:longitude\>147.804\</lm:longitude\>  
\</lm:coordinates\>  
\<lm:addressInfo\>  
\<lm:country\>Singapore\</lm:country\>  
\<lm:city\>Singapore\</lm:city\>  
\<lm:postalCode\>10\</lm:postalCode\>  
\<lm:district\>Bukit Merah\</lm:district\>  
\<lm:street\>Malan Road\</lm:street\>  
\<lm:phoneNumber\>+65987987987\</lm:phoneNumber\>  
\</lm:addressInfo\>  
\</lm:landmark\>  
\</lm:landmarkCollection\>  
\</lm:lmx\>

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [2 June 2008 06:19 UTC](https://rubytalk.org/t/parse-xml-to-database/46853/2 "2008-06-02T06:19:36Z")

</div>

I don't see any COMMIT in there.

&nbsp;&nbsp;robert

> **···**
>
> On 02.06.2008 04:06, Cean Ooi wrote:
> 
> > hi, can someone help me with this  
> > after i read theres nothing show in my database.... wat happen...?  
> > where is my error in my code??

---

<div class="post-metadata">

**Author:** ![Cean\_Ooi](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Cean\_Ooi](https://rubytalk.org/u/Cean_Ooi)\
**Post date:** [2 June 2008 06:30 UTC](https://rubytalk.org/t/parse-xml-to-database/46853/3 "2008-06-02T06:30:13Z")

</div>

hmm....i solved the database problem.. but... after i extract the xml  
file, it shows me nil

which part that i did wrong?

require 'rexml/document'  
require "mysql"  
require "dbi"  
include REXML  
scanfile = File.new('River\_Va.lmx')

dbname="email\_development"  
&nbsp;&nbsp;&nbsp;&nbsp;doc = REXML::Document.new scanfile

string = \<\<EOF  
&nbsp;&nbsp;\<mydoc\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<someelement attribute="nanoo"\>Text, text, text\</someelement\>  
&nbsp;&nbsp;\</mydoc\>  
EOF  
doc = Document.new string

doc = Document.new File.new("River\_Va.lmx")

&nbsp;&nbsp;&nbsp;&nbsp;doc.elements.each("lm:landmarkCollection/lm:landmark") { |element|  
puts element.attributes["lm:name"] }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;root = doc.root

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;popo = root.attributes["lm:name"]  
&nbsp;&nbsp;&nbsp;puts root.attributes["lm:name"]

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Cean\_Ooi](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Cean\_Ooi](https://rubytalk.org/u/Cean_Ooi)\
**Post date:** [2 June 2008 07:07 UTC](https://rubytalk.org/t/parse-xml-to-database/46853/4 "2008-06-02T07:07:21Z")

</div>

well.... i found it how.. but then, can anyone help me how to remove  
this thing inside the database please???

\<try\>123\</try\>

cause my attribute for my try in the database is integer, and it cant  
read the try, so how shud i remove that thing before the database??

really appreciate if someone would help me with this...

cheers

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Cean\_Ooi](https://avatars.discourse-cdn.com/v4/letter/c/848f3c/32.png) [@Cean\_Ooi](https://rubytalk.org/u/Cean_Ooi)\
**Post date:** [2 June 2008 07:07 UTC](https://rubytalk.org/t/parse-xml-to-database/46853/5 "2008-06-02T07:07:21Z")

</div>

remove the \<try\>\</try\>

.. thx for helping...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
