# Parse XML to database

**URL:** <https://www.ruby-forum.com/t/parse-xml-to-database/139225>\
**Category:** Ruby\
**Created:** [June 2, 2008, 4:07am UTC](https://www.ruby-forum.com/t/parse-xml-to-database/139225 "2008-06-02T04:07:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 2, 2008, 4:07am UTC](https://www.ruby-forum.com/t/parse-xml-to-database/139225/1 "2008-06-02T04:07:11Z")

</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

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()  
root = maps.root

names = []  
latitudes = []  
longtitudes = []

```
      #~ MSISDN = {}
#~ mobils.elements.each("MobileDevices/MobileDevice") { |element|

```

puts  
#~ element.attributes[“MSISDN”]  
#~ MSISDN = element.attributes[“MSISDN”] }

```
#~ datetime = {}
#~ mobils.elements.each("MobileDevices/MobileDevice") { |element|

```

puts  
#~ element.attributes[“datetime”]  
#~ datetime = element.attributes[“datetime”] }

```
#~ description = {}
#~ mobils.elements.each("MobileDevices/MobileDevice") { |element|

```

puts  
#~ element.attributes[“description”]  
#~ description = element.attributes[“description”] }

```
maps.elements.each("lm:landmark") { |element| puts

```

element.attributes[“name”]  
names.push element.attributes[“name”]  
name = element.attributes[“name”]}

puts name  
maps.elements.each(“lm:landmark”) { |element| puts  
element.attributes[“latitude”]  
latitudes.push element.attributes[“latitude”]}

puts latitudes  
maps.elements.each(“lm:longitude”) { |element| puts  
element.attributes[“longtitude”]  
longtitudes.push element.attributes[“longtitude”] }

puts longtitudes

# db insert

dbname=“email\_development”

m = Mysql.new(“localhost”, “root”, “”, “email\_development”)  
sth=m.query(“insert into maps (name,latitude,longtitude) values  
(name,latitude,longtitude)”)  
#~ (“INSERT INTO Maps (name, latitude,  
#~ longtitude)  
#~ VALUES (?,?,?,?,?)”)  
#~ models.each\_index do |index|  
#~name = names[index]  
#~ latitude = latitudes[index]  
#~ longtitude = longtitudes[index]  
# 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:nameRiver Valley High School\</lm:name\>  
lm:coordinates  
lm:latitude9.36\</lm:latitude\>  
lm:longitude147.804\</lm:longitude\>  
\</lm:coordinates\>  
lm:addressInfo  
lm:countrySingapore\</lm:country\>  
lm:citySingapore\</lm:city\>  
lm:postalCode10\</lm:postalCode\>  
lm:districtBukit Merah\</lm:district\>  
lm:streetMalan Road\</lm:street\>  
lm:phoneNumber+65987987987\</lm:phoneNumber\>  
\</lm:addressInfo\>  
\</lm:landmark\>  
\</lm:landmarkCollection\>  
\</lm:lmx\>

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 2, 2008, 8:20am UTC](https://www.ruby-forum.com/t/parse-xml-to-database/139225/2 "2008-06-02T08:20:58Z")

</div>

On 02.06.2008 04:06, Cean O. 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??

I don’t see any COMMIT in there.

robert

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 2, 2008, 8:30am UTC](https://www.ruby-forum.com/t/parse-xml-to-database/139225/3 "2008-06-02T08:30:48Z")

</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”  
doc = REXML::Document.new scanfile

string = \<\<EOF  
  
Text, text, text  
  
EOF  
doc = Document.new string

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

```
doc.elements.each("lm:landmarkCollection/lm:landmark") { |element| 

```

puts element.attributes[“lm:name”] }  
root = doc.root

```
  popo = root.attributes["lm:name"]

```

puts root.attributes[“lm:name”]

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 2, 2008, 9:07am UTC](https://www.ruby-forum.com/t/parse-xml-to-database/139225/4 "2008-06-02T09:07:20Z")

</div>

remove the

… thx for helping…

---

<div class="post-metadata">

**Author:** ![hloooi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/f17d59/32.png) [@hloooi](https://www.ruby-forum.com/u/hloooi)\
**Post date:** [June 2, 2008, 9:06am UTC](https://www.ruby-forum.com/t/parse-xml-to-database/139225/5 "2008-06-02T09:06:11Z")

</div>

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

123

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
