Xml to database

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
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation=“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>

On Jun 2, 4:31 am, Cean O. [email protected] 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??

well you’ve never actually done an insert with the data you extracted
from mysql. You could figure out the mysql stuff but it would be an
awful lot easier to use activerecord (which I assume you want to use
since you posted to the rails list): Map.create :name =>
name, :latitude => latitude, :longitude => longitude (assuming name,
latitude, longitude contain the desired values.
On top of that element.attributes[“name”] and similar aren’t going to
do much since no element there has a name attritbute (an attribute is
something like <sometag name="foo> …

Fred

hi fred, glad u here… im done with the database things… but now i
face a problem, which is, how im gonna remove the tags when i saved into
database…

example

1
the attribute for number is integer

but it has error when saving into database

unless i saved into another attribute which is string, then it will
shows 1

so how im gonna strip the tags away?
thx alot…

On Jun 2, 9:04 am, Cean O. [email protected] wrote:

hi fred, glad u here… im done with the database things… but now i
face a problem, which is, how im gonna remove the tags when i saved into
database…

example

1
the attribute for number is integer

That’s really not an attribute (in the xml sense), it’s a text node.
If you had the number element, then element.text would return 1. It
sounds like you should maybe read a few xml/rexml tutorials. If you’re
using an actual xml parser (as opposed to messing around with regexps
etc… which is really not a good idea) then you should never have to
‘strip the tags away’

Fred

oh… well… alright…

i dont think im using an actual xml parser…this is my code…

invisibility = XPath.first( doc, “/*/lm:landmark” )
XPath.each( doc, “//lm:coordinates”) { |element| puts element.text }
latitude = XPath.match( doc, “//lm:latitude” )
puts latitude

is this the actual one?

thx fred and im tryin now…

On Jun 2, 9:24 am, Cean O. [email protected] wrote:

oh… well… alright…

i dont think im using an actual xml parser…this is my code…

invisibility = XPath.first( doc, “/*/lm:landmark” )
XPath.each( doc, “//lm:coordinates”) { |element| puts element.text }
latitude = XPath.match( doc, “//lm:latitude” )
puts latitude

You are using a parser. If you’re ending up with things like
1 it’s probably because you’re outputting the whole
element (or in the case above latitude is an array of elements).

Fred

oh fred, and why if i use this method and it shows me nil?

doc.elements.each(“lm:lmx/lm:landmarkCollection/lm:landmark”) {
|element| puts element.attributes[“lm:name”] }

but if this

doc.elements.each(“lm:lmx”) { |element| puts
element.attributes[“xmlns:lm”] }

it shows me the data

erm… can explain this code pls?

im not really clear with it …

doc = Document.new(’<!DOCTYPE foo [ ]>lm:namereplace
&ent;</lm:name>’,{:raw=>:all})
doc.root.text

hoho… thx alot… sry for any inconvinient

oh i know y now… thx…

oh i jus realise it, the name is not an attribute, but how am i gonna
read the name using this method?