Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #3990 > unrolled thread

Import values from XML file

Started bySarah <dandi.box@gmail.com>
First post2011-11-30 15:30 -0800
Last post2012-03-27 10:26 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Import values from XML file Sarah <dandi.box@gmail.com> - 2011-11-30 15:30 -0800
    Re: Import values from XML file Hans Olo <doctor@vaderlishious.com> - 2011-11-30 20:21 -0600
      Re: Import values from XML file seeWebInstead@rem.intarweb.org (Robert Maas, http://tinyurl.com/uh3t) - 2012-03-27 10:26 -0700

#3990 — Import values from XML file

FromSarah <dandi.box@gmail.com>
Date2011-11-30 15:30 -0800
SubjectImport values from XML file
Message-ID<3fddac7a-3158-47e2-8d17-3420005bec4c@r28g2000yqj.googlegroups.com>
I've an XML file with this structure

<?xml version="1.0" encoding="UTF-8"?>
<azienda tabella="tb">
<riga IdAA="1" DesBB="Eliminazione" />
<riga IdAA="2" DesBB="Controllo" />
</azienda>

can you tell me how can I save each row in my DB?

I don't know how to create a function for read all values and save in
DB table

Thanks

[toc] | [next] | [standalone]


#3991

FromHans Olo <doctor@vaderlishious.com>
Date2011-11-30 20:21 -0600
Message-ID<DzBBq.1429$%O.1216@newsfe20.iad>
In reply to#3990
On 11/30/2011 5:30 PM, Sarah cried from the depths of the abyss:
> I've an XML file with this structure
>
> <?xml version="1.0" encoding="UTF-8"?>
> <azienda tabella="tb">
> <riga IdAA="1" DesBB="Eliminazione" />
> <riga IdAA="2" DesBB="Controllo" />
> </azienda>
>
> can you tell me how can I save each row in my DB?
>
> I don't know how to create a function for read all values and save in
> DB table

I do this quite often, and use PHP's DOM functions to read the .xml

http://php.net/manual/en/book.dom.php

Once you can read through the .xml file it's fairly basic how to insert 
into a table.

[toc] | [prev] | [next] | [standalone]


#5246

FromseeWebInstead@rem.intarweb.org (Robert Maas, http://tinyurl.com/uh3t)
Date2012-03-27 10:26 -0700
Message-ID<REM-2012mar27-002@Yahoo.Com>
In reply to#3991
> > I've an XML file with this structure
(structure snipped)
> > can you tell me how can I save each row in my DB?
> > I don't know how to create a function for read all values and save in
> > DB table
> From: Hans Olo <doc...@vaderlishious.com>
> I do this quite often, and use PHP's DOM functions to read the .xml

I agree, with one additional suggestion later below.

> http://php.net/manual/en/book.dom.php

Note that for this purpose of reading an existing XML file, most of
that manual can be ignored. All you need are the methods to tell
the children of a given node, and the type and (text) contents of
each node. All the modification methods are moot to your needs.

> Once you can read through the .xml file it's fairly basic how to
> insert into a table.

Now here's my additional suggestion: The XML DOM parser merely
validates that the file is valid XML, and converts it into a parse
tree. It doesn't verify that the structure (tree = DOM) matches
what you expect for your application. IMO it's dangerous to
traverse the tree node by node, copying data directly into DB rows,
because if there's any mistake in the XML, you've already blindly
copied earlier data into tables and thus have a table that isn't
completely updated, i.e. is an inconsistent mess. So what I do
instead is to validate the *entire* DOM node by node, copying all
the data into a tree o nested associative array. If I discover a
mistake in the structure after I've built part of the tree, no
trouble, I "die" and the nested associative array is discarded, and
the DB table is left exactly as it was at the start. If the
validation process runs to completion, then I know the tree is
entirely correct, and *then* I can copy the data from the nested
associative array into the DB tables, after which the DB tables
will again be in a consistent state.

It's an extra step the way I do it, but I believe it's safer.
But it's your choice whether you do that or not.

Google-groups-search-key: imtrgfdi

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web