Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Parse XML to database

Reply
Thread Tools

Parse XML to database

 
 
Cean Ooi
Guest
Posts: n/a
 
      06-02-2008
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:ocument.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"
xmlnssi="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: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>
<lmostalCode>10</lmostalCode>
<lm:district>Bukit Merah</lm:district>
<lm:street>Malan Road</lm:street>
<lmhoneNumber>+65987987987</lmhoneNumber>
</lm:addressInfo>
</lm:landmark>
</lm:landmarkCollection>
</lm:lmx>
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      06-02-2008
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??


I don't see any COMMIT in there.

robert
 
Reply With Quote
 
 
 
 
Cean Ooi
Guest
Posts: n/a
 
      06-02-2008
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:ocument.new scanfile

string = <<EOF
<mydoc>
<someelement attribute="nanoo">Text, text, text</someelement>
</mydoc>
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"]
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Cean Ooi
Guest
Posts: n/a
 
      06-02-2008
remove the <try></try>

.. thx for helping...
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Cean Ooi
Guest
Posts: n/a
 
      06-02-2008
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/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
newbie: How do I use Ruby on Rails to accept XML, parse it and store in a database? wchfilms@gmail.com Ruby 1 11-19-2006 10:26 PM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM



Advertisments