Results 1 to 23 of 23
  1. #1
    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 bowlsprinkle with 1/2 teaspoon bla bla bla
        
    </method>
    [...] 
    XML_Parser.php:
    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;

    ?>
    REQUIRE ADDITIONNAL FILES ATTACHED AT BOTTOM OF THIS POST


    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

  2. #2
    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.

  3. #3
    Join Date
    Oct 2004
    Location
    ALEXANDRIA_EGYPT
    Posts
    3
    poping elements from a stack is agood idea too

  4. #4
    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.

  5. #5
    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 bowlsprinkle with 1/2 teaspoon bla bla bla 
        
    </method>
    </
    recipe
    Use that xml. as for the output could it be something like
    PHP Code:
    //-----------------------------
    // loop thruh $recipe[x]
    echo $recipename;
    //loop thruh x's
    echo $ingredients[x];
    echo 
    $method;
    //----------------------------- 
    I'm desperate here
    Last edited by Wojtek; 01-09-2005 at 01:32 PM.

  6. #6
    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";

    Just edit the process_recipe function to make mysql_query calls
    + 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!

  7. #7
    I forgot to include this .zip.
    Attached Files Attached Files
    + 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!

  8. #8
    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!

  9. #9
    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!

  10. #10
    Join Date
    Sep 2003
    Location
    Montreal, Canada
    Posts
    374
    I tried both ways.
    Deleting the <recipes> tag made the script timeout

    awaiting your reply

  11. #11
    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!

  12. #12
    Join Date
    Sep 2003
    Location
    Montreal, Canada
    Posts
    374
    PM Sent

    Looking forward for that email

  13. #13
    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!

  14. #14
    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!

  15. #15
    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

  16. #16
    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!

  17. #17
    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?

  18. #18
    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!

  19. #19
    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.
    Dosnt specifically deny .zip's or does it?

  20. #20
    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!

  21. #21
    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

  22. #22
    Join Date
    Sep 2003
    Location
    Montreal, Canada
    Posts
    374
    I'm reporting back that it's working Wonderfully!!

    Thanks Alot, A++!

  23. #23
    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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •