Results 1 to 23 of 23
-
01-07-2005, 11:38 AM #1Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
XML -> HTML parse // How to -> MySQL?
XML Structure:
PHP Code:[...]
<recipe>
<recipename>Zucchini Dinner Rolls</recipename>
<serves>24</serves>
<keywords>Breads</keywords>
<ingredients>
<ingredient>
<quantity>1</quantity>
<unit>pkt</unit>
<name>quick-rise yeast</name>
</ingredient>
<ingredient>
<quantity>1</quantity>
<unit>cup</unit>
<name>warm water (120-130 degrees)</name>
</ingredient>
<ingredient>
<quantity>1/4</quantity>
<unit>cup</unit>
<name>butter or margarine softened</name>
</ingredient>
</ingredients>
<method>
Place zucchini in a bowl; sprinkle with 1/2 teaspoon bla bla bla
</method>
[...]
PHP Code:<?php
require("XMLConverter.class.php");
ob_start();
include("sample.xml");
$content=ob_get_contents();
ob_end_clean();
//Transform XML
$converter=new XMLConverter("taglib.tpl");
$content=$converter->parse($content);
$converter->destroyParser();
echo $content;
?>
The output is this: (XML_Parser.php)
http://www.cookingpages.com/cp/sample.php
Now, how would I modify all this so that each tag is seperated?
I want to transfer all those recipes into a MySql databse, but I need to break that down.
The last bit of XML_Parser.php:
echo $content;
I want to do this:
//DB Connect
Insert into DB ( recipename, servings, ingredients, method)
Any ideas how would I go to break this down?
Would I have to edit the function php files and add the database insert stuff there? I think so, however I'll need to make a bunch of IF's for each tag.
if recipename then insert into recipename
if ingredients insert into ingredients blablabla
not really effective no? Help
Thanks
-
01-08-2005, 03:13 AM #2Web Hosting Master
- Join Date
- Jul 2003
- Location
- Kuwait
- Posts
- 5,104
A few ways to approach this problem.
The easiest, imo -- is to simply parse the XML into a structure (like an array), then pop each element into the table.
Use http://pear.php.net/XML_Serializer to help you with that. Very easy stuff.
-
01-08-2005, 10:43 AM #3New Member
- Join Date
- Oct 2004
- Location
- ALEXANDRIA_EGYPT
- Posts
- 3
poping elements from a stack is agood idea too
-
01-08-2005, 05:34 PM #4Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
Thanks guys,
Actually I'v found MiniXML class. (http://minixml.psychogenic.com)
From what I'v read it could be good to what I need it for.
It's similar to PHP5's SimpleXML. I'll try that and post back If I run into any trouble.
-
01-09-2005, 01:28 PM #5Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
Failed miserably, cant do anything!
Could ANYONE PLEASE write me a short php snippet to actually get to that XML?
no error checking no nothing, only dirty php code.
This thing will only be used once to convert an xml databse to a mysql databse.
PHP Code:<recipe>
<recipename>Zucchini Dinner Rolls</recipename>
<serves>24</serves>
<keywords>Breads</keywords>
<ingredients>
<ingredient>
<quantity>1</quantity>
<unit>pkt</unit>
<name>quick-rise yeast</name>
</ingredient>
<ingredient>
<quantity>1</quantity>
<unit>cup</unit>
<name>warm water (120-130 degrees)</name>
</ingredient>
<ingredient>
<quantity>1/4</quantity>
<unit>cup</unit>
<name>butter or margarine softened</name>
</ingredient>
</ingredients>
<method>
Place zucchini in a bowl; sprinkle with 1/2 teaspoon bla bla bla
</method>
</recipe>
PHP Code://-----------------------------
// loop thruh $recipe[x]
echo $recipename;
//loop thruh x's
echo $ingredients[x];
echo $method;
//-----------------------------
Last edited by Wojtek; 01-09-2005 at 01:32 PM.
-
01-09-2005, 03:57 PM #6Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
Use MiniXML from http://minixml.psychogenic.com/
PHP Code:require_once('minixml.inc.php'); // Require MiniXML class
$file = 'xml.xml'; // The name of the file to parse
$parser = new MiniXMLDoc(); // Create new object
$parser->fromFile($file); // Read the file $file
$parsed_array = $parser->toArray(); // Parse the file into an array
if ($parsed_array['recipe']['recipename']) {
/* There is only one recipe. */
process_recipe($parsed_array['recipe']); // Process this recipe.
}
else {
/* There is more than one recipe! We need to look thru each */
foreach ($parsed_array['recipe'] as $key => $value) {
process_recipe($value); // Process this recipe.
}
}
function process_recipe($params) {
echo "<pre>";
echo "Recipe Name: " . $params['recipename'] . "\n";
echo "Serves: " . $params['serves'] . "\n";
echo "Keywords: " . $params['keywords'] . "\n";
foreach ($params['ingredients'] as $tmp => $ingred_array) {
foreach ($ingred_array as $tmp => $ingredient) {
echo "Iingredients:" . " Quantity: ".$ingredient['quantity'];
echo " Unit: ".$ingredient['unit'];
echo " Name: ".$ingredient['name'] . "\n";
}
}
echo "Method: " . $params['method'] . "\n\n";
}
+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 04:03 PM #7Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
I forgot to include this .zip.
+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 06:35 PM #8Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
Thanks mhale!
Its working excellent with that demo xml file. However It wont work with my .xml file.
I get:
Warning: Invalid argument supplied for foreach() in /home/wojtek/public_html/cp/minixml/New/xml.php on line 16
Thats:
foreach ($parsed_array['recipe'] as $key => $value) {
And yes my xml file does contain <recipe>...
Please take a look and tell me where is the problem.
Is it the code? is it the xml file? I'v tried many things to solve this. getting rid of the xml declare at the top, removing the <recipes> tag before all the <recipe> tags but then it would just freeze and return a timout error. grrrrr
Here's my XML file. Can you test it and tell your oppinion?
www.AllPoem.com/xml.zip
Thanks!
-
01-09-2005, 06:42 PM #9Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
I'll check it out within the next few mins.
BTW, if there is a <recipes> before the <recipe> it won't work using that script. <recipe> has to be the top level node.
For example:
<xml>
<recipe>
blahblah
</recipe>
<recipe>
blahblah
</recipe>
I'll take a look at your XML in a few.+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 06:56 PM #10Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
I tried both ways.
Deleting the <recipes> tag made the script timeout
awaiting your reply
-
01-09-2005, 07:32 PM #11Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
OK. MiniXML wasn't parsing it right. I switched to PEAR and some of its functions and it is working correctly now with the exact XML file you gave me (including the <recipes> tag as the top node).
The new file I need to give you with all the PEAR modules is too big for an attachment on WHT. What is your e-mail address so that I can send it to you? (You can PM me it if you wish).+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 07:38 PM #12Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
PM Sent
Looking forward for that email
-
01-09-2005, 07:42 PM #13Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
E-mail sent. Just edit the $file var to the name of your xml file (default is xml.xml).
BTW, Before you can import that <method> node into a DB, you will need to make sure its SQL safe (adding backslashes to things like ' and ")+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 07:44 PM #14Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
Hrm:
----- The following addresses had permanent fatal errors -----
<your email addr here(hidden)>
----- Transcript of session follows -----
... while talking to gsmtp185.google.com.:
>>> DATA
<<< 552 5.7.0 Illegal Attachment
554 <your email addr here(hidden)>... Service unavailable
Does gMail not allow .zip attachments?+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 07:47 PM #15Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
that would be lame if they didnt
try renaming to .txt and send again
I'll rename back to .zip here
-
01-09-2005, 07:58 PM #16Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
Ok. I resent it. Hopefully it will go thru all the way this time.
+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 08:03 PM #17Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
Pathetic...
It got trimmed to a 1Kb messed up file
perhaps you could upload it to your website to a /temp/ folder and pm me the url?
-
01-09-2005, 08:05 PM #18Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
Will do.
I can't believe gmail has all those problems with attachments.+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 08:10 PM #19Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
As a security measure to prevent potential viruses, Gmail does not allow you to receive executable files (such as files ending in .exe) that could contain damaging executable code.
Gmail does not accept these types of files, even if they are sent in a zipped (.zip, .tar, .tgz, .taz, .z, .gz) format. If someone tries to send this type of message to your Gmail account, the message will be bounced back to the sender.
-
01-09-2005, 08:13 PM #20Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
It told me illegal attachment. It was a .zip with mainly .php files inside it. There were no .exe files inside of that zip :/
+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!
-
01-09-2005, 08:31 PM #21Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
Weird... gmail has its issues I suppose...
Got your PM, will try what you sent me after dinner. Starving hehe
-
01-09-2005, 09:51 PM #22Aspiring Evangelist
- Join Date
- Sep 2003
- Location
- Montreal, Canada
- Posts
- 374
I'm reporting back that it's working Wonderfully!!
Thanks Alot, A++!
-
01-09-2005, 09:53 PM #23Web Hosting Guru
- Join Date
- Mar 2002
- Posts
- 266
Great to hear
+ Ahvio Networks :: webspace that works .. always
+ We'll beat anyone else's ModernBill Prices!
+ Reseller Accounts w/ Branded DemoDemo Tutorials
+ 99.99% Uptime / 60 Money-Back / Now 49 CP Skins!