|
Posted by Michael Trausch on 09/30/43 11:36
NC wrote:
[snip]
>
> Note that there are two <item> entities, but they contain completely
> different data fields. Moreover, XML can contain entities within
> entities, whereas databases cannot contain records within records...
>
> This problem can be solved only if the XML structure can be directly
> mapped onto a database...
>
It is possible depending on the back-end DB server. If you were using
something that supported row-like types for a field, you could manage to
make such a thing happen. It would be pretty complex though, and it
would require the logic to be smart enough to know when the DB
infrastructure existed to do such a thing.
If you're using a very tightly controlled source for the XML, you could
(in theory) use any of the XML parsing libraries or classes that are out
there, or try using PHP's built-in XML items. I have not yet used any
of these things myself, but if you have the tightly controlled XML
already, then you could pull the nodes that you know that you want to
populate the database with, and do something like (warning: pseudo-code):
---------
for each xml_node("item")
{
$sql = "INSERT INTO table (xx, yy, zz) VALUES ('";
$sql .= db_system_escape_string(xml_node("./xx"));
$sql .= "', '";
$sql .= db_system_escape_string(xml_node("./yy"));
$sql .= "', '";
$sql .= db_system_escape_string(xml_node("./zz"));
$sql .= "')";
db_system_execute_query($sql);
}
----------
That would be, however, harder in practice to implement then the
pseudo-code you see above. The logic of it would be simple: Iterate
over every <item> ... </item> node and extract the interesting data.
This makes broad assumptions about the XML that is coming in, however,
and since any well-formed XML document can be parsed, figuring out how
to regulate all of that is up to the OP.
Best wishes,
Mike
[Back to original message]
|