Jump to content

Archived

This topic is now archived and is closed to further replies.

Highlord

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

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  :(

Share this post


Link to post
Share on other sites
When you run the foreach loop... what does it print out?

Share this post


Link to post
Share on other sites

×

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.