Jump to content

PHP: Getting data out of an xml-file and write it to SQL-database


Highlord

Recommended Posts

Ok,

I have a XML-file with the following structure:

<row>
<field name="id">12</field>
<field name="car">VW</field>
<field name="year">1992</field>
<field name="type">4x4</field>
<field name="fuel">Gas</field>
</row>

Offcourse with multipe <row>'s.
Now I have to write a PHP-script that gets the id, car, and type out of this structure and assigns it to a variable so i can put this in sql-database table (carid, carname, type).

I allready found a way to show the data I want on the screen, I just cant figure out how i get it in the database.

This is how I print the selected data on a screen:

  <?php
 
    $mblist  = '';
    $flag    = false;
$type = 0;
$hgid = array();
$normid= array();
$perc_code = array();



    function openElement($parser, $element, $attributes) {
        global $flag;
        if (($element == 'field') && ($attributes['name'] == 'id'))
{
$flag = true;
$type = 1;
}
if (($element == 'field') && ($attributes['name'] == 'car'))
{
$flag = true;
$type = 2;
}
if (($element == 'field') && ($attributes['name'] == 'type'))
{
$flag = true;
$type = 3;
}
    }

    function closeElement($parser, $element) {
        global $flag;
        $flag = false;
    }

    function characterData($parser, $data) {
        global $flag,$mblist;
        if ($flag)  $mblist[] = $data;
    }

    $parser = xml_parser_create();

    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_element_handler($parser, "openElement", "closeElement");
    xml_set_character_data_handler($parser, "characterData");

    $document = file_get_contents("lassi.xml");
    xml_parse($parser, $document);

    xml_parser_free($parser);

    foreach ($mblist as $value) {
        echo $value.'<br/>';
    }

?>




I'd realy like some actual help with the scripting and not so much a link to a site or anything, because I've been trying to do this for 2days now and I think I've read every possible site about it, but just dont find how to do it  :(
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.