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

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

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

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

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

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?

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 <number>1</number> it's probably because you're outputting the whole element (or in the case above latitude is an array of elements).

Fred

thx fred and im tryin now...

erm... can explain this code pls?

im not really clear with it ...

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

hoho.. thx alot... sry for any inconvinient

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

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