469,271 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

from xml to mysql

with phpmyadmin i export a table of a mysql db in xml file. Now i want
import a xml file in a mysql db.
How can i do??
Thanks
Andrea
Jul 17 '05 #1
6 18305
Muppy wrote:
with phpmyadmin i export a table of a mysql db in xml file. Now i want
import a xml file in a mysql db.
How can i do??
Thanks
Andrea


I don't know phpmyadmin, but if it has a export to xml, does it not
have a import from xml to go along?

If it doesn't make your own :)
I don't know what your xml data looks like, but it's very
easy to transform

<sqldata>
<record>
<name>Pedro</name>
<eyecolor>brown</eyecolor>
</record>
<record>
<name>Muppy</name>
<eyecolor>blue</eyecolor>
</record>
<!-- ... ... ... -->
</sqldata>

into

insert into table (name, eyecolor) values ('Pedro', 'brown');
insert into table (name, eyecolor) values ('Muppy', 'blue');

and then mysql_query every one of the insert statments.
Happy Coding :-)
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #2
> I don't know phpmyadmin, but if it has a export to xml, does it not
have a import from xml to go along?
no, it doesn't have.
If it doesn't make your own :)
I don't know what your xml data looks like, but it's very
easy to transform


It's not esay for me ;)
Any help will be appreciated
Thanks
Andrea

Jul 17 '05 #3
Muppy wrote:
It's not esay for me ;)
Any help will be appreciated


Ever tried regexps?

Take that data

<?php
$data = "<?xml version=\"1.0\"?>
<sqldata>
<record><name>Pedro</name><eyecolor>brown</eyecolor></record>
<record><name>Muppy</name><eyecolor>blue</eyecolor></record>
</sqldata>";
?>

and get all the records into an array

<?php
preg_match_all('#<record>(.*)</record>#Us', $data, $records);
?>

then, for each record, get the respective values, and insert into sql
I am treating all values as strings (quoting them with ')
you might want to test the column names and do it differently

<?php
foreach ($records[1] as $record) {
preg_match_all('#<(.*)>(.*)</\1>#Us', $record, $values);
// $values[1] has the column names
// $values[2] has the values to insert
$sql = "insert into table ("
. implode(', ', $values[1])
. ") values ('"
. implode("', '", $values[2])
. "')";
echo $sql, '<br />'; ### or mysql_query($sql)
}
?>
Happy Coding :-)
check the regexp page at php.net
** http://www.php.net/manual/en/ref.pcre.php

--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #4
> Ever tried regexps?

no ;(
Many thanks for the help!
Andrea
Jul 17 '05 #5
On 4 Oct 2003 18:10:28 GMT, Pedro <he****@hotpop.com> wrote:
Muppy wrote:
It's not esay for me ;)
Any help will be appreciated


Ever tried regexps?


You can't parse XML fully with just regexps. Use an XML parser:
http://www.php.net/manual/en/ref.xml.php

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 17 '05 #6
Muppy wrote on Saturday 04 October 2003 12:24:
Ever tried regexps?


no ;(
Many thanks for the help!
Andrea


Either use XML parser in PHP per Andy's suggestion or use ActiveLink PHP XML
Package to manipulate XML (no PHP XML libs required):

http://www.active-link.com/intranet/software.php
http://php-xml.sourceforge.net/

Good luck!

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Nils Valentin | last post: by
reply views Thread by amg | last post: by
1 post views Thread by Khue Pham | last post: by
1 post views Thread by DD | last post: by
7 posts views Thread by Randy | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.